gpt4 book ai didi

c++ - 自定义对象的常量表达式

转载 作者:行者123 更新时间:2023-11-30 01:00:31 24 4
gpt4 key购买 nike

我正在尝试使用自定义类的实例作为模板参数。

class X {
public:
X() {};
};

template <class Foo, Foo foo>
struct Bar {

};
const X x;
Bar<X, x> foo;

编译器声明 x 不能出现在常量表达式中。为什么?在编译时为构造该对象提供了一切。

最佳答案

你做不到。标准 14.1 说:

4 A non-type template-parameter shall have one of the following (optionally cv-qualified) types:
— integral or enumeration type,
— pointer to object or pointer to function,
— reference to object or reference to function,
— pointer to member.
5 [ Note: other types are disallowed either explicitly below or implicitly by the rules governing the form of template-arguments (14.3). —end note ] The top-level cv-qualifiers on the template-parameter are ignored when determining its type.

关于c++ - 自定义对象的常量表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2521869/

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