Error overloading functions with enable_if

lodo

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.

Jarod42

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; }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related