【发布时间】:2017-01-12 21:01:02
【问题描述】:
考虑以下 sn-p 来测试即将推出的 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 上都失败了,消息是:
分解声明不能声明为'constexpr'
删除constexpr 定义并更改为常规assert() 对两种编译器都有效。
关于此功能的 WG21 论文均未提及 constexpr 关键字,无论是正面的还是负面的。
问题:为什么不允许分解声明为constexpr? (除了“因为标准这么说”)。
【问题讨论】:
-
"以前称为结构化绑定" ???它们现在仍然被称为“结构化绑定” (eel.is/c++draft/dcl.struct.bind) 我错过了一些历史性的东西吗?
标签: c++ constexpr c++17 structured-bindings