为什么分解声明不能是constexpr? [英] Why can't decomposition declarations be constexpr?

查看:241
本文介绍了为什么分解声明不能是constexpr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码段,以测试即将到来的C ++ 17功能分解声明(以前称为结构化绑定)

Consider the following snippet to test the upcoming C++17 feature decomposition declarations (formerly known as structured bindings)

#include <cassert>
#include <utility>

constexpr auto divmod(int n, int d)
{
    return std::make_pair(n / d, n % d); // in g++7, also just std::pair{n/d, n%d}
}

int main()
{
    constexpr auto [q, r] = divmod(10, 3);
    static_assert(q == 3 && r ==1);
}

在g ++ 7-SVN和clang-4.0-SVN上均失败并显示以下消息:

This fails on both g++7-SVN and clang-4.0-SVN with the message:


分解声明不能声明为'constexpr'

decomposition declaration cannot be declared 'constexpr'

删除 constexpr 定义并更改为常规的 assert()均可在两个编译器上使用。

Dropping the constexpr definition and changing to a regular assert() works on both compilers.

关于此功能的WG21论文都没有提到 constexpr 关键字,无论是肯定的还是否定的。

None of the WG21 papers on this feature mention the constexpr keyword, neither in the positive nor the negative.

问题:为什么不允许分解声明为 constexpr ? (除了因为标准这样说。)

Question: why aren't decomposition declarations be allowed to be constexpr? (apart from "because the Standard says so").

推荐答案


问题:为什么不这样做分解声明被允许为constexpr? (除了因为标准这样说。)

Question: why aren't decomposition declarations be allowed to be constexpr? (apart from "because the Standard says so").

没有其他原因。该标准在[dcl.dcl] p8中说:

There is no other reason. The standard says in [dcl.dcl] p8:


decl-specifier-seq 仅应包含类型说明符 自动(7.1.7.4)和 cv限定符

The decl-specifier-seq shall contain only the type-specifier auto (7.1.7.4) and cv-qualifiers.

这意味着它不能用 constexpr 声明。

That means it can't be declared with constexpr.

这是国家机构对C ++ 17 CD的评论,请参见 P0488R0

This was the subject of a National Body comment on the C++17 CD, see US-95 in P0488R0:


注释:没有明显的原因为什么不能将分解
声明声明为静态,
thread_local或constexpr。

建议的更改: 允许constexpr,static和thread_local到
允许的 decl-specifiers 集中。

Comment: There is no obvious reason why decomposition declarations cannot be declared as static, thread_local, or constexpr.
Proposed change: Allow constexpr, static, and thread_local to the permitted set of decl-specifiers.

评论GB 16和GB 17也相关。

Comments GB 16 and GB 17 are also related.

在经过Evolution Working Group在2016年11月的会议上审查后,这些评论被C ++ 17拒绝。 。目前尚不清楚某些存储类对结构化绑定声明的含义,以及如何更改规范以允许 constexpr (仅在语法中允许它不会说什么)尚不清楚。它的意思是)。要求探讨设计空间的论文。将来应该有可能在不破坏任何代码的情况下进行更改,但是对于C ++ 17来说没有时间这样做。

These comment were rejected for C++17 after review by the Evolution Working Group at the Nov 2016 meeting. It was unclear what some storage classes would mean on a structured binding declaration, and exactly how to change the specification to allow constexpr (simply allowing it in the grammar wouldn't say what it means). A paper exploring the design space was requested. It should be possible to change this in future without breaking any code, but there wasn't time to do it for C++17.

这篇关于为什么分解声明不能是constexpr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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