为什么分解声明不能是 constexpr?

Why can't decomposition declarations be constexpr?

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

#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 上均失败并显示消息:

decomposition declaration cannot be declared 'constexpr'

删除 constexpr 定义并更改为常规 assert() 适用于两种编译器。

None 关于此功能的 WG21 论文中提到了 constexpr 关键字,无论是正面的还是负面的。

问题:为什么不允许分解声明是constexpr? (除了 "because the Standard says so")。

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

没有别的原因。标准在 [dcl.dcl] p8:

中说

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

也就是说不能用constexpr声明。

这是国家机构对 C++17 CD 评论的主题,请参阅 P0488R0 中的 US-95:

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 也相关。

进化工作组在 2016 年 11 月的会议上审查后,这些评论被 C++17 拒绝。目前尚不清楚一些存储 类 在结构化绑定声明中意味着什么,以及如何更改规范以允许 constexpr(仅在语法中允许它并不能说明它的含义)。要求撰写一篇探讨设计 space 的论文。将来应该可以在不破坏任何代码的情况下改变它,但没有时间为 C++17 做这件事。