【问题标题】:Lambda in branch not taken of constant expression: Who is right?分支中的 Lambda 不采用常量表达式:谁是对的?
【发布时间】:2017-12-19 21:59:44
【问题描述】:

我尝试编译以下 C++11 代码,结果好坏参半。

struct NoTemplate {
    static constexpr auto (*foo)() = false ? +[]{} : nullptr;
};

NoTemplate no_inst;


template<typename>
struct YesTemplate {
    static constexpr auto (*foo)() = false ? +[]{} : nullptr;
};

YesTemplate<float> yes_inst;
  • clang:成功编译NoTemplate;在YesTemplate 上给error: a lambda expression may not appear inside of a constant expression
  • gcc: 编译成功
  • msvc:崩溃。
  • icc:崩溃(我们有赢家!)

正确的结果是什么?我看到一些标准语言建议非常量表达式在常量表达式中短路运算符的错误分支中应该是可以的,但是 IANALL。

【问题讨论】:

  • 崩溃是指编译器崩溃还是程序崩溃?
  • 在标准设置为 c++17 且警告设置为 4 级的最新 VC (19.12.something) 中,我在“+”上得到了模棱两可的运算符
  • 看起来像一个clang错误,clang HEAD不提供诊断see it ive语言supports lambdas in unevaluated portion of conditional
  • @user4581301 编译器崩溃。
  • 好吧,我们可以肯定地说编译器崩溃总是一个错误,而不是预期的行为。 :) 不过,找到多次崩溃的工作做得很好。

标签: c++ c++11 lambda language-lawyer


【解决方案1】:

这个程序格式正确。

注意,在 C++17 之后,核心常量表达式可以接受 lambda 表达式,即使它会被计算。详情可以看论文N4487

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 2021-12-20
    • 2011-04-19
    • 2011-06-23
    • 2017-03-30
    • 1970-01-01
    • 2021-10-30
    相关资源
    最近更新 更多