相关疑难解决方法(0)

为什么constexpr静态成员(类型类)需要定义?

==>查看coliru上的完整代码段和编译.

我有一个LiteralType类填充constexpr要求:

struct MyString
{
    constexpr MyString(char const* p, int s) : ptr(p), sz(s) {}
    constexpr char const* data() const { return ptr; }
    constexpr int         size() const { return sz;  }

    char const *ptr = 0;
    int  const  sz  = 0;
};
Run Code Online (Sandbox Code Playgroud)

我用它作为constexpr static成员变量:

struct Foo
{
    int size() { return str_.size(); }

    constexpr static MyString str_{"ABC",3};
};

int main()
{
  Foo foo;
  return ! foo.size();
}
Run Code Online (Sandbox Code Playgroud)

但链接器说:(
Clang-3.5和GCC-4.9)

undefined …
Run Code Online (Sandbox Code Playgroud)

c++ static-members constexpr c++11 c++14

10
推荐指数
1
解决办法
5654
查看次数

标签 统计

c++ ×1

c++11 ×1

c++14 ×1

constexpr ×1

static-members ×1