mercredi 14 novembre 2018

Template function argument deduction with enable_if - reference to pointer

Maybe there is good solution that work on g++ 4.6.{3,4} ? You can check in on https://godbolt.org/

#include <type_traits>
class A{};
class B{};
class C{
    public:
    A* a;
    B* b;
};

template<typename T, typename std::enable_if<std::is_same<typename std::remove_reference<T>::type,A*>::value>::type* = nullptr >
void f(T&& t) { 
    return;
}

int main() { 
    C c;
    auto& cRef = c;
    f(cRef.a);
    f(c.a);
}

g++ /tmp/enable_if.cpp -std=c++0x

/tmp/enable_if.cpp: In function ‘int main()’:
/tmp/enable_if.cpp:20:13: error: no matching function for call to ‘f(A*&)’
/tmp/enable_if.cpp:20:13: note: candidate is:
/tmp/enable_if.cpp:13:6: note: template<class T, typename std::enable_if<std::is_same<typename std::remove_reference<_MemPtr>::type, A*>::value, void>::type* <anonymous> > void f(T&&)
/tmp/enable_if.cpp:21:10: error: no matching function for call to ‘f(A*&)’
/tmp/enable_if.cpp:21:10: note: candidate is:
/tmp/enable_if.cpp:13:6: note: template<class T, typename std::enable_if<std::is_same<typename std::remove_reference<_MemPtr>::type, A*>::value, void>::type* <anonymous> > void f(T&&)

Aucun commentaire:

Enregistrer un commentaire