#include <string>
#include <vector>
#include <iostream>
#include <loki/TypeTraits.h>
enum copy_algorithm_selector { normal, fast };
template <typename source, typename target>
target
copy_impl(source first, source last, target result, Loki::Int2Type<normal>)
{
std::cout << "normal" << std::endl;
for (; first != last; ++first, ++result)
{
*result = *first;
}
return result;
}
template <typename source, typename target>
target
copy_impl(source first, source last, target result, Loki::Int2Type<fast>)
{
std::cout << "fast" << std::endl;
const std::size_t n = last-first;
memcpy(result, first, n);
return result + n;
}
template <typename source, typename target>
target
do_copy(source first, source last, target result)
{
typedef typename Loki::TypeTraits<source>::PointeeType source_pointee;
typedef typename Loki::TypeTraits<target>::PointeeType target_pointee;
enum
{
copy_algorithm =
Loki::TypeTraits<source>::isPointer &&
Loki::TypeTraits<target>::isPointer &&
Loki::TypeTraits<source_pointee>::isStdFundamental &&
Loki::TypeTraits<target_pointee>::isStdFundamental &&
sizeof(source_pointee) == sizeof(target_pointee) ? fast : normal
};
return copy_impl(first, last, result, Loki::Int2Type<copy_algorithm>());
}
void
normal_copy_test()
{
typedef std::vector<int> intvec;
intvec source;
intvec target;
source.push_back(1); source.push_back(2); source.push_back(3);
do_copy<intvec::iterator, std::back_insert_iterator<intvec> >(
source.begin(),
source.end(),
std::back_inserter(target));
}
void
fast_copy_test()
{
int source[] = { 1, 2, 3, 4, 5, 6 };
int target[6];
do_copy<int*, int*>(source, source + (6*sizeof(int)), target);
}
int
main()
{
normal_copy_test();
fast_copy_test();
return 0;
}