gpt4 book ai didi

C++,没有默认构造函数的默认参数

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:24:05 27 4
gpt4 key购买 nike

标准对这种情况怎么说:

template<class C>
struct A {
A(C c = C());
};

struct C {
C(int);
};

A<C> a(C(1));

顺便说一句,Comeau 不会引发错误。

最佳答案

如果您不使用默认参数(即您提供一个值),则它根本不会被实例化。

14.7.1/2:

Unless a function template specialization has been explicitly instantiated or explicitly specialized, the func- tion template specialization is implicitly instantiated when the specialization is referenced in a context that requires a function definition to exist. Unless a call is to a function template explicit specialization or to a member function of an explicitly specialized class template, a default argument for a function template or a member function of a class template is implicitly instantiated when the function is called in a context that requires the value of the default argument.

真是一口。实际上,函数和模板的默认参数都有自己特定的实例化规则。

关于C++,没有默认构造函数的默认参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3662450/

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