相关疑难解决方法(0)

静态constexpr odr-used or not?

为什么下面的工作原理gcc却没有clang,(现场观看):

constexpr int giveMeValue() { return 42; }

struct TryMe {
  static constexpr int arr[1] = {
      giveMeValue()
  };  
};

int main() {
    int val = TryMe::arr[0];
    return val;
}
Run Code Online (Sandbox Code Playgroud)

我得到一个未解决的外部符号与clang.

TryMe::arr[0]一个对象?如果是的话,它是否经常使用?

c++ one-definition-rule constexpr c++11

9
推荐指数
1
解决办法
1758
查看次数

C++类是否可以在头文件中包含内联初始化的静态const std :: array?

这就是我所拥有的:

struct Foo
{
   static std::array<double, 4> acgt_default_background_frequencies() { return {0.281774, 0.222020, 0.228876, 0.267330}; }
};
Run Code Online (Sandbox Code Playgroud)

但我宁愿不使用函数而只是有一个变量,如下所示:

struct Foo
{
   static constexpr std::array<double, 4> acgt_default_background_frequencies = {0.281774, 0.222020, 0.228876, 0.267330};
};
Run Code Online (Sandbox Code Playgroud)

我想要编译,但当我尝试使用 Foo::acgt_default_background_frequencies它时,链接器错误"未定义引用`Foo :: acgt_default_background_frequencies'".

我正在尝试做什么?我认为我的标题的读者更清楚,如果我将值内联为const而不是将其隐藏在.cpp文件中并且具有常量而不是函数也看起来更清晰.constexpr是不是允许这样的东西?如果不可能,为什么不呢?

c++ c++11

5
推荐指数
1
解决办法
2425
查看次数

如何在静态constexpr类成员中使用make_tuple()?

我想用make_tuple()constexpr.它适用于全球范围.但它会为static constexpr类成员生成链接错误.

#include <iostream>
#include <tuple>

using namespace std;

class A
{
public:
    static constexpr auto z = make_tuple(5, 3.0);
};

constexpr auto tp = make_tuple(6, 3.2);

int main()
{

    cout << get<0>(tp) << " " << get<1>(tp) << endl; // OK

    cout << get<0>(A::z) << "  " << get<1>(A::z) << endl; // error: (.text+0x5a): undefined reference to `A::z'
                                                          //        (.text+0x67): undefined reference to `A::z'

}
Run Code Online (Sandbox Code Playgroud)

我已经检查了这里 make_tuple本身不是constexprc++11.我猜这不是问题.如果是,它将生成编译错误而不是链接错误. …

c++ constexpr c++11

2
推荐指数
1
解决办法
549
查看次数

标签 统计

c++ ×3

c++11 ×3

constexpr ×2

one-definition-rule ×1