Open Eisenwave opened 1 year ago
template<class T> struct SimplyCopyable : std::false_type {};
template<> struct SimplyCopyable<int> : std::true_type {};
template<class Iter>
Out trivially_copy(Iter first, Iter last, Iter out)
{
// use memmove
}
template<class Iter>
Out algo_copy(Iter first, Iter last, Iter out)
{
// use loop calling copy constructors
}
template<class Iter>
Out copy(Iter first, Iter last, Iter out)
{
if constexpr (SimplyCopyable<ValueType<Iter>>::value) {
trivially_copy(first, last, out);
}
else {
algo_copy(first, last, out);
}
}
This is a more practical implementation using a constexpr if statement. Tag dispatch doesn't profit from boolean type traits much; we need tags. This is especially bad considering that type traits in the standard library are virtually all boolean. One of the only cases where we have tags is in std::iterator_traits
.
In modern C++, I don't see a reason to use tag dispatch in the example of T.65:
Tag Dispatch
constepxr if (Strawman)
Even if you are using a tag, you can just as well check the tag with
if constexpr
and dispatch to functions with distinct names. In my opinion, uses of tag dispatch like above are antiquated and should no longer be recommended.The second version:
I fail to see any legitimate reason why you would use tag dispatch in C++17 here. In C++20 you might also just create overloads with constraints like
template <pod_type T>
, which also seems easier and cleaner than tag dispatch.