奇怪的重复模板模式(CRTP)与静态constexpr在Clang [英] Curiously recurring template pattern (CRTP) with static constexpr in Clang
本文介绍了奇怪的重复模板模式(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!
这篇关于奇怪的重复模板模式(CRTP)与静态constexpr在Clang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文