相关疑难解决方法(0)

为什么函数参数类型中使用的模板参数包作为其模板参数列表无法显式指定

我有以下代码:

template <typename, typename>
struct AAA{};

template<typename ...Args>
void f(AAA<Args...> *) {}

int main() {
    f<int, int>(nullptr);
}
Run Code Online (Sandbox Code Playgroud)

此代码导致编译错误.使用g++ -std=c++1z错误编译时显示如下:

prog.cc: In function 'int main()':
prog.cc:8:24: error: no matching function for call to 'f<int, int>(std::nullptr_t)'
     f<int, int>(nullptr);
                        ^
prog.cc:5:6: note: candidate: template<class ... Args> void f(AAA<Args ...>*)
 void f(AAA<Args...> *) {}
      ^
prog.cc:5:6: note:   template argument deduction/substitution failed:
prog.cc:8:24: note:   mismatched types 'AAA<Args ...>*' and 'std::nullptr_t'
     f<int, int>(nullptr);
Run Code Online (Sandbox Code Playgroud)

使用clang++ -std=c++1z错误是:

prog.cc:8:5: error: no matching function for …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++17

39
推荐指数
4
解决办法
1641
查看次数

标签 统计

c++ ×1

c++17 ×1

templates ×1