gpt4 book ai didi

c++ - 在其自己的定义中隐式实例化模板

转载 作者:行者123 更新时间:2023-12-05 06:44:34 24 4
gpt4 key购买 nike

让我们考虑有关模板隐式实例化的简单示例:

#include <iostream>
template<int N>
class A
{
static const int a = A<N-1>::a; //1, OK, doesn't require implicit intantiation
};

template<int N>
class B
{
static const int a = B<1>::a; //2, Error, implicit instantiation of template 'B<1>' within its own definition
};


int main(){ }

DEMO

标准并不清楚这一事实。它说的只是 N3797::14.7.1/1 [temp.inst]:

Unless a class template specialization has been explicitly instantiated (14.7.2) or explicitly specialized (14.7.3), the class template specialization is implicitly instantiated when the specialization is referenced in a context that requires a completely-defined object type or when the completeness of the class type affects the semantics of the program.

1 和 2 都不需要完全定义类类型,但是第二个会导致有关隐式实例化的错误。我想了解原因。

最佳答案

我觉得原因B<1>立即失败并且A<N-1>不是因为temp.res#general-8.4

The validity of a template may be checked prior to any instantiation. The program is ill-formed, no diagnostic required, if:

a hypothetical instantiation of a template immediately following its definition would be ill-formed due to a construct that does not depend on a template parameter

关于c++ - 在其自己的定义中隐式实例化模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28715062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com