constexpr使用静态函数初始化静态成员 [英] constexpr initializing static member using static function
问题描述
我想要一个 constexpr
值(即编译时常数) c $ c> constexpr 函数。我希望这两个作用域都属于类的命名空间,即静态方法和类的静态成员。
I want a constexpr
value (i.e. a compile-time constant) computed from a constexpr
function. And I want both of these scoped to the namespace of a class, i.e. a static method and a static member of the class.
我第一次写到这个(对我)明显的方式:
I first wrote this the (to me) obvious way:
class C1 {
constexpr static int foo(int x) { return x + 1; }
constexpr static int bar = foo(sizeof(int));
};
g ++ - 4.5.3 -std = gnu ++ 0x
说:
error: ‘static int C1::foo(int)’ cannot appear in a constant-expression
error: a function call cannot appear in a constant-expression
g ++ - 4.6.3 -std = gnu ++ 0x
抱怨:
error: field initializer is not constant
第二次尝试
OK,我想,也许我必须把事情从课堂上移开。所以我尝试以下:
Second attempt
OK, I thought, perhaps I have to move things out of the class body. So I tried the following:
class C2 {
constexpr static int foo(int x) { return x + 1; }
constexpr static int bar;
};
constexpr int C2::bar = C2::foo(sizeof(int));
g ++ - 4.5.3
没有投诉。不幸的是,我的其他代码使用一些基于范围的 for
循环,所以我必须至少有4.6。现在我更靠近支持列表,看起来 constexpr
也需要4.6。 g ++ - 4.6.3
我得到
g++-4.5.3
will compile that without complaints. Unfortunately, my other code uses some range-based for
loops, so I have to have at least 4.6. Now that I look closer at the support list, it appears that constexpr
would require 4.6 as well. And with g++-4.6.3
I get
3:24: error: constexpr static data member ‘bar’ must have an initializer
5:19: error: redeclaration ‘C2::bar’ differs in ‘constexpr’
3:24: error: from previous declaration ‘C2::bar’
5:19: error: ‘C2::bar’ declared ‘constexpr’ outside its class
5:19: error: declaration of ‘const int C2::bar’ outside of class is not definition [-fpermissive]
这听起来真的很奇怪。如何做事情在 constexpr
不同?我不喜欢添加 -fpermissive
,因为我更喜欢我的其他代码被严格检查。将类别主体之外的 foo
实现移除没有可见的效果。
This sounds really strange to me. How do things "differ in constexpr
" here? I don't feel like adding -fpermissive
as I prefer my other code to be rigurously checked. Moving the foo
implementation outside the class body had no visible effect.
有人能解释这里发生了什么吗?我如何实现我想要做什么?我主要对以下类型的答案感兴趣:
Can someone explain what is going on here? How can I achieve what I'm attempting to do? I'm mainly interested in answers of the following kinds:
- 使用gcc-4.6工作的方法
- 一个观察,后来的gcc版本可以正确地处理一个版本
- 指向spec的指针,根据这个指针,我的至少一个构造
- 根据规范,我想要的信息是不可能的,最好是有一些不确定的背后的理由此限制
- A way to make this work in gcc-4.6
- An observation that later gcc versions can deal with one of the versions correctly
- A pointer to the spec according to which at least one of my constructs should work, so that I can bug the gcc developers about actually getting it to work
- Information that what I want is impossible according to the specs, preferrably with some insigt as to the rationale behind this restriction
其他有用的答案也是欢迎的,但也许不会被轻易接受。
Other useful answers are welcome as well, but perhaps won't be accepted as easily.
推荐答案
标准要求(第9.4.2节):
The Standard requires (section 9.4.2):
可以在类定义中使用
constexpr
说明符声明文本类型的static
数据成员;如果是,其声明应指定括号或初始值,其中每个 initializer-clause 是常量表达式。
A
static
data member of literal type can be declared in the class definition with theconstexpr
specifier; if so, its declaration shall specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression.
在你的第二次尝试和Ilya的回答代码中,声明没有 -or-equal-initializer 。
In your "second attempt" and the code in Ilya's answer, the declaration doesn't have a brace-or-equal-initializer.
您的第一个代码是正确的。不幸的是gcc 4.6不接受它,我不知道在哪里方便地试用4.7.x(例如ideone.com仍然停留在gcc 4.5)。
这是不可能的,因为不幸的是,标准排除了在类完成的任何上下文中初始化一个静态的 constexpr
数据成员。 9.2p2中 brace-or-equal-initializers 的特殊规则仅适用于非静态数据成员,但这一个是静态的。
This isn't possible, because unfortunately the Standard precludes initializing a static constexpr
data member in any context where the class is complete. The special rule for brace-or-equal-initializers in 9.2p2 only applies to non-static data members, but this one is static.
最可能的原因是, constexpr
变量必须作为编译时常量表达式从成员函数体内部获得,因此变量初始化器在函数体之前完全定义 - 这意味着函数在初始化器的上下文中仍然是不完整的(未定义的),然后这个规则开始,使得表达式不是常量表达式:
The most likely reason for this is that constexpr
variables have to be available as compile-time constant expressions from inside the bodies of member functions, so the variable initializers are completely defined before the function bodies -- which means the function is still incomplete (undefined) in the context of the initializer, and then this rule kicks in, making the expression not be a constant expression:
调用未定义的
constexpr
函数或未定义的constexpr
constexpr 函数或constexpr
构造函数的定义之外的构造函数
an invocation of an undefined
constexpr
function or an undefinedconstexpr
constructor outside the definition of aconstexpr
function or aconstexpr
constructor;
请考虑:
class C1
{
constexpr static int foo(int x) { return x + bar; }
constexpr static int bar = foo(sizeof(int));
};
这篇关于constexpr使用静态函数初始化静态成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!