问题描述
这里是我的代码:
对 test / code>:class test {
public:
constexpr test(){
}
constexpr int operator +(const test& rhs)const {
return x + 1;
}
int x = 10;
};
尝试在 operator + 我们可以看到以下行现在失败:
constexpr int b = t + test $ cl $ b从clang发出以下错误(
):error:constexpr variable'b'必须用常量表达式初始化
constexpr int b = t + test(); //在编译时工作!
^ ~~~~~~~~
注意:在常量表达式中不允许读取非constexpr变量't'
return x + 1;
^
失败,因为 t 不是constexpr变量,因此其子对象也不是constexpr变量。
您的第二个示例:
constexpr int c = w + 2;
不工作,因为它属于草案C ++ 11标准节 5.19 [expr.const] :
Here is my code:
class test{ public: constexpr test(){ } constexpr int operator+(const test& rhs){ return 1; } }; int main(){ test t; //constexpr word isn't necessary constexpr int b = t+test(); // works at compile time! int w = 10; // ERROR constexpr required constexpr int c = w + 2; // Requires w to be constexpr return 0; }I notice that it worked even though I didn't specify test to be constexpr. I tried replicating the result by doing the same with int but i get errors. Specifically, it wants my int w inside the constexpr int c = w + 2; to be constexpr. From my first attempt which is using test, Did it work because of the reason that i used constexpr on the constructor already? If that is the case then would it good to assume that all classes that have constexpr on their constructors will result to all objects instantiated or created with it to be constexpr?
Bonus question:
If I have a constexpr constructor, is it bad to do something like ? test * t = new test(); ?
解决方案Having a constexpr constructor does not make declarations of that variable automatically constexpr, so t is not a constexpr. What is going on in this case is that you are calling a constexpr function, this line:
constexpr int b = t+test();can be viewed as follows:
constexpr int b = t.operator+( test() );So then the question is whether test() is a constant expression, which it is since the constructor is constexpr and does not fall under any of the exceptions under the draft C++11 standard section 5.19 [expr.const] paragraph 2 which says:
and includes the following bullet:
We can see this more readily by making some small changes to test by introducing a member variable x:
class test{ public: constexpr test(){ } constexpr int operator+(const test& rhs) const { return x + 1 ; } int x = 10 ; };Attempting to access it in operator + and we can see that the following line now fails:
constexpr int b = t+test();with the following error from clang (see it live):
error: constexpr variable 'b' must be initialized by a constant expression constexpr int b = t+test(); // works at compile time! ^ ~~~~~~~~ note: read of non-constexpr variable 't' is not allowed in a constant expression return x + 1 ; ^It fails because t is not a constexpr variable and therefore its subobjects are also not constexpr variables.
Your second example:
constexpr int c = w + 2;does not work because it falls under one of the exceptions in the draft C++11 standard section 5.19 [expr.const] :
这篇关于在构造函数上指定constexpr会自动使从它创建的所有对象都是constexpr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!