The code below correctly checks if the type T
has a method sort
. But when I modify the line marked (*)
by changing decltype(&U::sort,...)
to decltype(U::sort,...)
(the symbol &
is removed), then the code returns always false
.
Why?
Why the name itself is not enough? What does this &
mean?
#include <iostream>
#include <type_traits>
template <typename T>
class has_sort {
template <typename U>
static auto check(bool) -> decltype(&U::sort, std::true_type()); // (*)
template <typename U>
static std::false_type check(...);
public:
using type = decltype(check<T>(true));
static bool const value = type::value;
};
int main() {
struct Foo { void sort(); };
struct Foo2 { void sort2(); };
std::cout << "Foo: " << has_sort<Foo>::value << std::endl;
std::cout << "Foo2: " << has_sort<Foo2>::value << std::endl;
std::cout << "int: " << has_sort<int>::value << std::endl;
}
Aucun commentaire:
Enregistrer un commentaire