Reading the Chapter 22 of C++ templates, Second Edition, I try to understand the implementation of the EqualityComparable trait. But I cannot understand how the compiler decide to activate the fallback or not.
In addition to this there are two function that have only declared but the program compiles and runs. This is strange to me.
Here is the code. The header file IsEqualityComparable.hpp
#include <utility> // for declval()
#include <type_traits> // for true_type and false_type
template<typename T>
class IsEqualityComparable
{
private:
// test convertibility of == and ! == to bool:
static void* conv(bool); // to check convertibility to bool
template<typename U>
static std::true_type test(decltype(conv(std::declval<U const&>() ==
std::declval<U const&>())),
decltype(conv(!(std::declval<U const&>() ==
std::declval<U const&>())))
);
// fallback:
template<typename U>
static std::false_type test(...);
public:
static constexpr bool value = decltype(test<T>(nullptr,
nullptr))::value;
};
The source file is the following
#include <iostream>
#include <exception>
#include <utility>
#include <functional>
#include "isequalitycomparable.hpp"
template<typename T,
bool EqComparable = IsEqualityComparable<T>::value>
struct TryEquals
{
static bool equals(T const& x1, T const& x2) {
std:: cout << "IsEqualityComparable equals::"<<std::endl;
return x1 == x2;
}
};
class NotEqualityComparable : public std::exception
{
};
template<typename T>
struct TryEquals<T, false>
{
static bool equals(T const& x1, T const& x2) {
std:: cout << "Throw::"<<std::endl;
throw NotEqualityComparable();
}
};
void foo(int)
{
}
void bar(int)
{
}
class A
{
public:
A() = default;
friend bool operator ==(A a1 , A a2)
{
return true;
}
};
int main()
{
std:: cout << "Enter" << std::endl;
std::function<void(int)> f = foo;
std::function<void(int)> f2 = f;
std:: cout << "Enter" << std::endl;
//std:: cout << "Check::"<<
//TryEquals<std::function<void(int)>>::equals(f,f2) << std::endl;
A a1;
A a2;
std:: cout << "Check::"<< TryEquals<A>::equals(a1,a2) << std::endl;
return 0;
}
The
TryEquals<std::function<void(int)>>::equals(f,f2)
throws an exception because the operator == is not implemented but
TryEquals<A>::equals(a1,a2)
returns 1 because the class A has an operator ==.
In this point I need help to understand how the conv and test work.
Moreover how does the
static constexpr bool value = decltype(test<T>(nullptr,
nullptr))::value
works?
I confused with this expression
decltype(test<T>(nullptr,nullptr))::value.