简体   繁体   English

模板参数中的派生 class 类型无法编译

[英]Derived class type in template argument doesn't compile

This is the code snippet I have been hopelessly stuck on.这是我一直无可救药的代码片段。

template <class T, T nt>
class C;

struct base{
   int i;
} b;

struct derived : base{} d;

C<base*,&d> obj;

Why this giving error could not convert template argument &d to base* ?为什么这个给出错误could not convert template argument &d to base*

When matching an argument to a parameter that is a pointer/reference, derived to base conversions are not considered even if the conversions are valid in other circumstances.在将参数与作为指针/引用的参数匹配时,即使转换在其他情况下有效,也不会考虑派生到基本转换。

14.3/5 [Standard quote just for reference] 14.3/5 [标准报价仅供参考]

If a non-type template-argument cannot be converted to the type of the corresponding template-parameter then the program is ill-formed.如果非类型模板参数不能转换为相应模板参数的类型,则该程序格式错误。

.... ……

for a non-type template-parameter of type pointer to object, qualification conversions (4.4) and the array-to-pointer conversion (4.2) are applied.对于指向 object 的类型指针的非类型模板参数,应用限定转换 (4.4) 和数组到指针转换 (4.2)。 [Note: In particular, neither the null pointer conversion (4.10) nor the derived-to-base conversion (4.10) are applied . [注意:特别是,null 指针转换 (4.10)和派生到基址转换 (4.10) 均未应用 Although 0 is a valid template-argument for a non-type template-parameter of integral type, it is not a valid template-argument for a non-type template-parameter of pointer type.尽管 0 是整数类型的非类型模板参数的有效模板参数,但它不是指针类型的非类型模板参数的有效模板参数。 ] ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自Template参数的派生类没有受保护的成员访问权限 - Derived class from Template argument doesn't have protected member access 在地图中获取不了解模板参数的派生类的模板参数类型 - Get template parameter type of derived class in map that doesn't know about template parameters 基类模板实例化取决于派生类构造函数的参数类型 - Base class template instantiation depending on derived class constructor argument type 如何确定在编译时是否从模板类派生类型? - How to determine if a type is derived from a template class at compile time? 派生类中的模板函数参数 - Template function argument in derived class 作为模板类型参数,为什么不键入[N]匹配其专用版本 - 模板 <class T> S级 <T[]> - As template type argument, why doesn't type[N] match its specialized version -— template<class T> class S<T[]> 模板中的函数类型无法编译 - Function type in template doesn't compile 删除不必要的模板参数,但不再编译 - Removing unnecessary template argument, but doesn't compile anymore 如何在返回类型功能模板的专业化中使用派生类型? (“无法推断模板参数”) - How to use derived type in specialisation of return-type function template? (“couldn't infer template argument”) 在派生类中使用基类的模板参数 - Using template argument of base class in derived class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM