gpt4 book ai didi

c++ - 与模板中的定义相对应的声明

转载 作者:行者123 更新时间:2023-11-28 00:18:20 25 4
gpt4 key购买 nike

N4296::14.7.1/1 [temp.inst]提供了以下示例:

template<class T, class U>
struct Outer {
template<class X, class Y> struct Inner;
template<class Y> struct Inner<T, Y>; // #1a
template<class Y> struct Inner<T, Y> { }; // #1b; OK: valid redeclaration of #1a
template<class Y> struct Inner<U, Y> { }; // #2
};
Outer<int, int> outer; // error at #2

并给出如下解释:

Outer<int, int>::Inner<int, Y> is redeclared at #1b. (It is not defined but noted as being associated with a definition in Outer<T, U>.) #2 is also a redeclaration of #1a. It is noted as associated with a definition, so it is an invalid redeclaration of the same partial specialization.

#1b 令我感到困惑被视为声明,而不是定义。我们在那里明确提供了函数体,为什么它仍然不是一个定义?事实上,你不能解释一下这个解释吗。

最佳答案

这在示例之前的文本中有解释!

The implicit instantiation of a class template specialization causes the implicit instantiation of the declarations, but not of the definitions or default arguments, of the class member functions, member classes, scoped member enumerations, static data members and member templates; and it causes the implicit instantiation of the definitions of unscoped member enumerations and member anonymous unions. However, for the purpose of determining whether an instantiated redeclaration of a member is valid according to 9.2, a declaration that corresponds to a definition in the template is considered to be a definition.

实例化模板产生的函数定义与函数模板本身的定义是不同的。

关于c++ - 与模板中的定义相对应的声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28814973/

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