jeudi 23 août 2018

SFINAE Based traits to determine if operator + is supported

Ηello I write a code in C++ using templates and I try to implement a SFINAE what is fired when operator + is not supported. I write the following code

#include <iostream>
#include <type_traits>
class B
{
};
template<typename T1,typename T2>
struct IsSameT :std::false_type
{
    //static constexpr bool value = false;
};

template<typename T>
struct IsSameT<T,T> :std::true_type
{
    //static constexpr bool value = true;
};
template<typename T1, typename T2>
struct HasPlusT
{
    private:
    using T1_t = typename std::remove_reference<T1>::type;
    using T2_t = typename std::remove_reference<T2>::type;
    template<typename U1, typename = decltype(T1_t() + T2_t())>
    static char test(void *);
    template<typename>
    static long test(...);
    public:
    static constexpr  bool value = IsSameT<decltype(test<T1,T2>(nullptr)),char>::value;
};

template<typename T1,typename T2, bool = HasPlusT<T1,T2>::value>
struct PlusResultT
{
    using T1_t = typename std::remove_reference<T1>::type;
    using T2_t = typename std::remove_reference<T2>::type;
    using Type = decltype(T1_t() + T2_t());
};

template<typename T1,typename T2>
struct PlusResultT<T1,T2,false>
{
    using T1_t = typename std::remove_reference<T1>::type;
    using T2_t = typename std::remove_reference<T2>::type;
    using Type = decltype(T1_t() + T2_t());
};


int main()
{

    constexpr bool value = HasPlusT<B,B>::value;
    return 0;
}

I expect the constexpr bool value = HasPlusT<B,B>::value to return false but an error is generated What is wrong in implementation?

Class B has not an operator + and I expect that the constexpr bool value = >HasPlusT::value returns true. However a compilation error is generated no match for 'operator+' (operand types are 'HasPlusT::T1_t {aka B}' and >'HasPlusT::T2_t {aka B}') Demo.cpp /Demo line 100 C/C++ >Problem

Aucun commentaire:

Enregistrer un commentaire