【发布时间】:2020-10-25 16:54:24
【问题描述】:
如何将 typedef 从一个类传递给它的 mixin?起初我以为可能是命名冲突,但在 mixin 中重命名 value_t 也无济于事。
template <typename Derived>
class Mixin
{
public:
using value_t = typename Derived::value_t;
Derived * self()
{
return static_cast<Derived *>(this);
}
value_t x() const
{
return self()->x;
}
};
class DerivedInt : public Mixin<DerivedInt>
{
public:
using value_t = int;
value_t x = 0;
};
class DerivedDouble : public Mixin<DerivedDouble>
{
public:
using value_t = double;
value_t x = 0.0;
};
clang 语义问题:
file.h:14:39: error: no type named 'value_t' in 'DerivedInt'
file.h:27:27: note: in instantiation of template class 'Mixin<DerivedInt>' requested here
file.h:14:39: error: no type named 'value_t' in 'DerivedDouble'
file.h:34:30: note: in instantiation of template class 'Mixin<DerivedDouble>' requested here
【问题讨论】: