为什么下面的工作原理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]一个对象?如果是的话,它是否经常使用?
这就是我所拥有的:
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是不是允许这样的东西?如果不可能,为什么不呢?
我想用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本身不是constexpr在c++11.我猜这不是问题.如果是,它将生成编译错误而不是链接错误. …