online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
#include <iostream> #include <algorithm> #include <vector> #include <type_traits> // indicates that the type doesn't have a tag type (like pointers and standard iterators) struct no_tag{}; namespace detail { template <typename T> auto tag_helper(int) -> typename T::tag; template <typename> auto tag_helper(long) -> no_tag; } // get T::tag or no_tag if T::tag isn't defined. template <typename T> using tag_t = decltype(detail::tag_helper<T>(0)); namespace N { struct my_iterator_tag {}; struct A{ using tag = my_iterator_tag; }; struct B{ using tag = my_iterator_tag; }; struct C{ using tag = my_iterator_tag; }; } namespace N { template<class SomeN1, class SomeN2> SomeN2 copy_helper(SomeN1 first, SomeN1 last, SomeN2 d_first, no_tag) { std::cout << "calling std::copy\n"; return std::copy(std::forward<SomeN1>(first), std::forward<SomeN1>(last), std::forward<SomeN2>(d_first)); } template<class SomeN1, class SomeN2> SomeN2 copy_helper(SomeN1 first, SomeN1 last, SomeN2 d_first, my_iterator_tag) { // your custom copy std::cout << "custom copy function\n"; return {}; } template<class SomeN1, class SomeN2> SomeN2 copy(SomeN1 first, SomeN1 last, SomeN2 d_first) { return copy_helper(std::forward<SomeN1>(first), std::forward<SomeN1>(last), std::forward<SomeN2>(d_first), tag_t<SomeN1>{}); } } template<class It1, class It2> void do_something(It1 first, It1 second, It2 d_first) { N::copy(first, second, d_first); } int main() { N::A a1, a2, a3; std::cout << "using custom iterator: "; do_something(a1, a2, a3); std::cout << "using vector iterator: "; std::vector<int> v; do_something(std::begin(v), std::end(v), std::begin(v)); std::cout << "using pointer: "; int* ptr = new int[10]; do_something(ptr, ptr + 5, ptr); return 0; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue