【发布时间】:2013-07-10 19:03:42
【问题描述】:
我正在尝试在编译时初始化一些 C++ 数组,但我遇到了一个奇怪的 g++ 错误。这是我能得到的最小的代码块,它重现了错误:
#include <array>
template<typename Ar, int... Vals>
constexpr Ar Map(typename Ar::value_type /*int*/ fun(int))
{ return {{ fun(Vals)... }}; }
constexpr int add(int i) { return i + 1; }
constexpr auto b = Map<std::array<int, 2>, 1, 2>(add);
编译器在抱怨
bug.cpp:8:53: in constexpr expansion of ‘Map<std::array<int, 2ul>, {1, 2}>(add)’
bug.cpp:4:80: error: expression ‘add’ does not designate a constexpr function
constexpr Ar Map(typename Ar::value_type /*int*/ fun(int)) { return {{ fun(Vals)... }}; }
g++ 4.7.1 和 4.9.0 20130520(实验性)都会发生这种情况。注意
如果我在定义中将typename Ar::value_type 替换为int(见评论)
Map,一切正常。这是我做错了什么的错误吗?
【问题讨论】:
标签: c++ c++11 variadic-templates type-safety constexpr