SFINAE可以调用

时间:2017-12-28 22:15:38

标签: c++ c++17 sfinae

我正在尝试创建一个模板函数,该函数仅用于匹配特定i模式的invocable(在示例中,接受int并返回int)。我试过这个,但它没有编译。任何人都可以向我解释原因吗?

#include <iostream>
#include <type_traits>

int foo(int) {return 0;}
int foo(char*) {return 0;}

template<class T>
std::enable_if_t<std::is_invocable_r_v<int, T, int>>
add(T t) {}

int main()
{
    add(foo);
}

我得到的错误(VS2017):

main.cpp(13): error C2672: 'add': no matching overloaded function found
main.cpp(13): error C2783: 'enable_if<_Test,_Ty>::type add(T)': could not deduce template argument for 'T'
        with
        [
            _Ty=void
        ]
main.cpp(9): note: see declaration of 'add'

0 个答案:

没有答案