Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error overloading functions with enable_if

I have this piece of code, with a function that has a different implementation based on what members are available:

#include <vector>

template <typename T>
struct D
{
    struct inner
        { T first; };
};

template <>
struct D<int>
{
    using inner = std::vector<int>;
};

template <typename T>
class C
{
    using B = D<T>;
    typename B::inner b;

    public:
        typename std::enable_if<std::is_same<decltype(std::declval<B::inner>().first),T>::value,T>::type
        first()
            { return b.first; }

        typename std::enable_if<std::is_same<decltype(std::declval<B::inner>()[0]),T>::value,T>::type
        first()
            { return b[0]; }
};

This code does not compile. gcc says that <first signature> cannot be overloaded with <second signature>. I don't understand why this does not work.

Thank you very much.

like image 284
lodo Avatar asked Oct 08 '15 10:10

lodo


1 Answers

SFINAE works on immediate context for template function. Here the functions are not template, it is the class which is.

One possible solution is to make your function template:

template <typename U = T>
std::enable_if_t<std::is_same<U, decltype(std::declval<B::inner>().first)>::value, T>
first()
{ return b.first; }
like image 80
Jarod42 Avatar answered Oct 06 '22 00:10

Jarod42