有没有一种方法可以定义一个非动态构造函数来限制默认构造函数的范围
struct foo {
int *bar;
};
static __thread foo myfoo[10] = {nullptr};也就是说,我想做的是
class baz {
public:
baz() = default;
constexpr baz(decltype(nullptr)) : qux(nullptr) { }
private:
int *qux;
};
static __thread baz mybaz[10] = {nullptr};让它正常工作。
目前,icpc告诉我
main.cpp(9): error: thread-local variable cannot be dynamically initialized
static __thread baz mybaz[10] = {nullptr};
^发布于 2012-11-18 05:48:36
这一点:
static __thread baz mybaz[10] = {nullptr};等同于:
static __thread baz mybaz[10] = {baz(nullptr), baz(), baz(), baz(), baz(), ..., baz()};因为这是默认情况下数组元素的隐式初始化是构造函数的一般规则。
因此,要么这样做:
static __thread baz mybaz[10] = {nullptr, nullptr, nullptr, ..., nullptr};或者让你的默认构造函数也常量表达式...
https://stackoverflow.com/questions/13336609
复制相似问题