相关疑难解决方法(0)

C++标准是否指定在某些情况下编译失败并出现错误?

我正在检查缩小转换的标准,我认为对于缩小转换,应该触发错误.因为标准说:

[注意:如上所述,列表初始化中的顶层不允许进行此类转换. - 结束说明]

我认为"不允许"的描述意味着编译应该失败.

但有人告诉我,这里只是说"程序格式不正确",标准不要求编译必须失败.

如果需要缩小转换(见下文)将元素转换为T,则程序格式不正确.

所以我的问题是:标准是否指定是否应该生成错误或警告?或者在某些情况下编译应该失败?从编译器的角度来看,编译程序是否可以,只是给出一些警告?

BTW:Clang 4.0.0Gcc 7.0.0表现不同.

float a {1.e39}; // Error for both Clang and GCC
double d;
float a3{d};     // Error for Clang, warning for GCC
Run Code Online (Sandbox Code Playgroud)

c++ iso type-conversion language-lawyer

25
推荐指数
3
解决办法
1382
查看次数

带auto的initializer_list包含多个表达式

相当简单的问题,

auto x11 {1,2,3,4};
auto x1 = {1,2,3,4};
auto x22 {1.0, 2.25, 3.5};
auto x2 = {1.0, 2.25, 3.5};
Run Code Online (Sandbox Code Playgroud)

据我了解,这里应该没有区别=.但是,使用llvm/clang 6.0.0(使用--std = c ++ 17),我得到:

main1.cpp:35:17: error: initializer for variable 'x11' with type 'auto' contains multiple
  expressions
auto x11 {1,2,3,4};
~~~~~~~~    ^

main1.cpp:37:20: error: initializer for variable 'x22' with type 'auto' contains multiple
  expressions
auto x22 {1.0, 2.25, 3.5};
Run Code Online (Sandbox Code Playgroud)

从Stroustroup的C++书籍,第162页:

auto x1 {1,2,3,4}; // x1 is an initializer_list<int>
auto x2 {1.0, 2.25, 3.5 }; // x2 is an …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer auto c++17

23
推荐指数
1
解决办法
893
查看次数

标签 统计

c++ ×2

language-lawyer ×2

auto ×1

c++17 ×1

iso ×1

type-conversion ×1