我试图在C ++类中实现constexpr成员函数,该函数返回模板参数。该代码应该与c ++ 11兼容。但是,当模板化的类还包含STL容器作为数据成员(如std :: vector)时,会遇到编译问题(constexpr成员函数未涉及)。
以下代码给出了一个最小的示例:
#include <vector>
#include <iostream>
#include <array>
template<size_t n>
struct A
{
constexpr size_t dimensions() const
{
return n;
}
private:
std::vector<double> a;
};
int main(int argc,char ** argv)
{
auto a=A<3>();
std::array<double,a.dimensions()> arr;
}
Run Code Online (Sandbox Code Playgroud)
该代码可以使用命令正确编译
g ++ -std = c ++ 14 -O3 quickTest.cpp -o test -Wall
clang ++ -std = c ++ 11 -O3 quickTest.cpp -o test -Wall
但是当我使用失败
g ++ -std = c ++ 11 -O3 quickTest.cpp -o test -Wall
与错误 …