模板参数推导失败和函数参数/参数不匹配 [英] Template Argument Deduction Failure and Function Parameters/Arguments Mismatch

查看:128
本文介绍了模板参数推导失败和函数参数/参数不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下程序:

template <class T> struct A { using X = typename T::X; };
template <class T, typename A<T>::X* = nullptr> void f(T, int);
void f(...);
template <class T> void g(T, int, typename A<T>::X* = nullptr); // #
void g(...);

int main() {
  // f(0, nullptr); // error
  g(0, nullptr); // ok       
}

g(0, nullptr)编译而f(0, nullptr)不编译(在 Godbolt ).似乎在#的模板参数推导过程中,当编译器发现参数nullptr与参数int不匹配时,不会实例化A<int>.标准在哪里指定这种行为?

g(0, nullptr) compiles while f(0, nullptr) does not (tested under GCC trunk and Clang trunk on Godbolt). It seems that during the template argument deduction process of #, the compiler does not instantiate A<int> when it finds the argument nullptr does not match the parameter int. Where does the standard specify this behavior?

推荐答案

这是 CWG1391 :

如果对包含以下所有参数的推导成功 参与模板参数推导的 template-parameters ,所有模板参数均已明确指定,推导或 从默认模板参数获取,其余参数为 然后与相应的参数进行比较.对于每个剩余的 参数P,其类型在替换之前是非依赖的 任何显式指定的模板参数(如果对应) 参数A不能隐式转换为P,推导失败.

If deduction succeeds for all parameters that contain template-parameters that participate in template argument deduction, and all template arguments are explicitly specified, deduced, or obtained from default template arguments, remaining parameters are then compared with the corresponding arguments. For each remaining parameter P with a type that was non-dependent before substitution of any explicitly-specified template arguments, if the corresponding argument A cannot be implicitly converted to P, deduction fails.

这篇关于模板参数推导失败和函数参数/参数不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆