奇怪的重复模板模式(CRTP)与静态constexpr在Clang

问题描述:

请看下面的简单示例:

#include <iostream>

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

class Derived : public Base<Derived>
{
public:
    static constexpr int x = 5;
};


int main()
{
    std::cout << Derived::y << std::endl;
}

在g ++中,这会编译精细并打印 5 。然而,在Clang中,它无法编译错误没有成员名为'x'在'Derived'。据我所知,这是正确的代码。

In g++, this compiles fine and prints 5 as expected. In Clang, however, it fails to compile with the error no member named 'x' in 'Derived'. As far as I can tell this is correct code. Is there something wrong with what I am doing, and if not, is there a way to have this work in Clang?

是否有办法在Clang中进行这项工作?这可能不是任何人都会寻找的答案,但我通过添加第三个类解决了这个问题:

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

#include <iostream>

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

class Data
{
public:
     static constexpr int x = 5;
};

class Derived : public Base<Data>, public Data {};

int main()
{
    std::cout << Derived::y << std::endl;
}

它可以按照需要工作,但不幸的是,它并没有真正的好处CRTP!

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