Template member functions with trailing return type, giving errors even if unused

偶尔善良 提交于 2019-12-03 15:07:24

SFINAE only applies to the template arguments of the function, not the one inherited from the class.

A different solution is to include copy T to a second template argument but this is nothing more than a shorter version of your workaround:

#include <utility>
#include <type_traits>    
struct Foo {
    template < typename T > T f() { return {}; }
};
template <typename T>
struct S {
    template <typename A, typename TT = T >
    auto f1()  -> decltype(std::declval<TT>().template f <A>()) { 
        static_assert(std::is_same<T,TT>::value, "TT must be equal to T" );
        return TT().template f <A>(); 
    }
};

int main() {
    S<Foo> a;
    a.f1<int>(); // ok

    S<int> b;
    b.f1<int>(); // not ok
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!