相关疑难解决方法(0)

为什么分解声明不能成为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);
}
Run Code Online (Sandbox Code Playgroud)

这在g ++ 7-SVN和clang-4.0-SVN上都失败了,并带有以下消息:

分解声明不能声明'constexpr'

删除constexpr定义并更改为assert()两个编译器上的常规工作.

关于此功能的WG21论文constexpr均未提及关键字,无论是正面还是负面.

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

c++ constexpr c++17 structured-bindings

38
推荐指数
1
解决办法
3649
查看次数

标签 统计

c++ ×1

c++17 ×1

constexpr ×1

structured-bindings ×1