我有一个CRTP模板类在这里:铛VS GCC CRTP:constexpr变量不能有非文字类
template <typename S>
class Base
{
public:
constexpr static S NOT_SET{0};
};
struct Derived : public Base<Derived>
{
};
锵(5.0.0)不接受这一点:
5 : <source>:5:24: error: constexpr variable cannot have non-literal type 'const Derived'
constexpr static S NOT_SET{0};
^
8 : <source>:8:25: note: in instantiation of template class 'Base<Derived>' requested here
struct Derived : public Base<Derived>
^
5 : <source>:5:24: note: incomplete type 'const Derived' is not a literal type
constexpr static S NOT_SET{0};
^
8 : <source>:8:8: note: definition of 'Derived' is not complete until the closing '}'
struct Derived : public Base<Derived>
^
1 error generated.
Compiler exited with result code 1
但海湾合作委员会(测试4.9.2和6.2)接受它就好了。
如何在叮当中这样做?
谢谢:但是,如果我试图移植一个现有的代码库,它利用了GCC的这个“特性”来叮当,你的建议是什么? – Anton
只要派生类型是constexpr可构造的,就可以在基类中定义一个constexpr函数,它返回它的_not set_版本(不管它是什么意思)。它可以为你工作吗? – skypjack
是的,这是个好主意。谢谢。我会接受你的回答,但是你认为你也可以把你的评论放入答案中,作为后代吗? :-) – Anton