Curiously recurring template pattern (CRTP) with static constexpr in Clang

前端 未结 2 651
轮回少年
轮回少年 2021-02-15 04:11

Consider my simple example below:

#include 

template 
class Base
{
public:
    static constexpr int y = T::x;
};

class Derive         


        
2条回答
  •  青春惊慌失措
    2021-02-15 04:42

    This probably isn't the answer anyone would be looking for, but I solved the problem by adding a third class:

    #include 
    
    template 
    class Base
    {
    public:
        static constexpr int y = T::x;
    };
    
    class Data
    {
    public:
         static constexpr int x = 5;
    };
    
    class Derived : public Base, public Data {};
    
    int main()
    {
        std::cout << Derived::y << std::endl;
    }
    

    It works as desired, but unfortunately it doesn't really have the benefits of CRTP!

提交回复
热议问题