这个constexpr整数不是一个空指针常量吗? [英] Is this constexpr integer not a null pointer constant?

查看:134
本文介绍了这个constexpr整数不是一个空指针常量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的C ++ 11程序,其结果在GCC 4.7.2中:

  int main b $ b {
constexpr int i = 0;
int * p = i;
}

// g ++ -g -ggdb -Wall -Wextra -pedantic -std = c ++ 11 t.cpp
// t.cpp:In function'int main()':
// t.cpp:4:13:错误:从'int'到'int *'无效转换[-fpermissive]
// t.cpp:4:警告:未使用的变量'p'[-Wunused-variable]

根据标准:


[C ++ 11:4.10 / 1]: 是一个整数常数表达式(5.19),整数类型的prvalue值为零[..]


我不能完全解析它,但我们不会期望 i 满足这个标准,并作为一个空指针常量因此不需要为 p


$ b的初始化显式转换 int * $ b

如果I s / constexpr / const / ,编译成功,并使用 -ansi code> -std = c + + 11 。

解决方案

p> [C ++ 11:5.19 / 3]:字面常量表达式是字面值类型的核心常量表达式,类型。 整数常数表达式是整数或无范围枚举类型的文字常量表达式。 [..]


和:


[C ++ 11:3.9 / 10]: 类型是字符类型 / p>


  • 标量类型;或

  • 引用类型;或

  • 一个类类型(第9条),它具有以下所有属性:[..]

  • li>

此时,我找不到该代码不符合SSL规定的原因,因此我怀疑有一个GCC错误。



然而,它可能是一个故意的错误,因为你引用4.10的段落被提议改变( active issue#903 ),因此这实际上是不符合SSL规定的代码。







如果I s / constexpr / const / ,并使用 -ansi 而不是 -std = c ++ 11 / p>

的定义明确允许在C ++ 03中使用这种情况:


[C ++ 03:5.19 / 1]: [..]常量表达式(8.5),积分的非类型模板参数(8.5),非常量表达式的非类型模板参数(8.5),常数表达式(或枚举类型,以及 sizeof 表达式。 [..]



Consider the following C++11 program, and its result in GCC 4.7.2:

int main()
{
   constexpr int i = 0;
   int* p = i;
}

// g++ -g -ggdb -Wall -Wextra -pedantic -std=c++11 t.cpp
// t.cpp: In function 'int main()':
// t.cpp:4:13: error: invalid conversion from 'int' to 'int*' [-fpermissive]
// t.cpp:4:9: warning: unused variable 'p' [-Wunused-variable]

According to the standard:

[C++11: 4.10/1]: A null pointer constant is an integral constant expression (5.19) prvalue of integer type that evaluates to zero [..]

5.19 is a mess and I've failed to parse it fully, but wouldn't we expect i to satisfy this criterion and act as a null pointer constant, consequently requiring no explicit conversion to int* for the initialisation of p?

The compilation succeeds if I s/constexpr/const/ and compile with -ansi rather than -std=c++11.

解决方案

[C++11: 5.19/3]: A literal constant expression is a prvalue core constant expression of literal type, but not pointer type. An integral constant expression is a literal constant expression of integral or unscoped enumeration type. [..]

And:

[C++11: 3.9/10]: A type is a literal type if it is:

  • a scalar type; or
  • a reference type; or
  • a class type (Clause 9) that has all of the following properties: [..]
  • an array of literal type.

At this point, I can't find a reason for that code to be non-compliant, so I suspect a GCC bug.

However it may be a deliberate bug given that the passage you quoted out of 4.10 is proposed to be changed (active issue #903) so that this would in fact be non-compliant code.


The compilation succeeds if I s/constexpr/const/ and compile with -ansi rather than -std=c++11.

The definition of integral constant expression explicitly allowed this case in C++03:

[C++03: 5.19/1]: [..] An integral constant-expression can involve only literals (2.13), enumerators, const variables or static data members of integral or enumeration types initialized with constant expressions (8.5), non-type template parameters of integral or enumeration types, and sizeof expressions. [..]

这篇关于这个constexpr整数不是一个空指针常量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆