小编Luc*_*isi的帖子

C ++中具有std :: vector数据成员的constexpr成员函数

我试图在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

与错误 …

c++ constexpr c++11

23
推荐指数
1
解决办法
764
查看次数

标签 统计

c++ ×1

c++11 ×1

constexpr ×1