How to test if a type T has a begin<T> defined
I have a SFINAE context where I have the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
struct have_sort { char c; };
struct have_range { char c[2]; };
struct have_none { char c[3]; };
template<typename T> have_sort test_sort(decltype(&T::sort), decltype(&T::sort));
template<typename T> have_range test_sort(decltype(&T::begin), decltype(&T::end));
template<typename T> have_none test_sort(...);
template<typename T, size_t s> struct fast_sort_helper;
// specialization 1
template<typename T>
struct fast_sort_helper<T, sizeof(have_sort)>
{
static void sort(T& x)
{
x.sort();
}
};
// specialization 2
template<typename T>
struct fast_sort_helper<T, sizeof(have_range)>
{
static void sort(T& x)
{
std::sort(x.begin(), x.end());
}
};
// specialization 3
template<typename T>
struct fast_sort_helper<T, sizeof(have_none)>
{
static void sort(T& x)
{
static_assert(sizeof(T)< 0, "No sort available");
}
};
template<typename T>
void fast_sort(T& t)
{
fast_sort_helper<T, sizeof(test_sort<T>(nullptr,nullptr))>::sort(t);
}
|
How do I test for the presence of a free function begin, the one that works on containers?
I tried this:"
|
template<typename T> long test_sort(decltype(&begin<T>), decltype(&begin<T>));
|
but it does not compile...
Regards,
Juan
Topic archived. No new replies allowed.