【发布时间】:2015-02-17 21:38:20
【问题描述】:
谁能解释一下为什么下面的代码不能编译:
template <typename P>
struct Base
{
friend typename P::One;
friend typename P::Two;
friend typename P::Three;
};
template<typename U, typename D, typename T>
struct Derived : public Base<Derived<U,D,T>>
{
using One = U;
using Two = D;
using Three = T;
};
错误是:
..\PRP\main.cpp:3:1: error: no type named 'One' in 'struct Derived<A, B, C>'
{
^
..\PRP\main.cpp:3:1: error: no type named 'Two' in 'struct Derived<A, B, C>'
..\PRP\main.cpp:3:1: error: no type named 'Three' in 'struct Derived<A, B, C>'
以及为什么以下代码可以完美编译:
template <typename T>
struct Sum
{
constexpr static int sze = T::uno + T::due + T::tre;
};
template<int i, int j, int k>
struct Triple : public Sum<Triple<i, j, k>>
{
constexpr static int uno = i;
constexpr static int due = j;
constexpr static int tre = k;
};
两者有什么区别?我认为这与模板扣除顺序有关,但我可能错了。
我在 Win7 上使用 MinGW 4.8 并开启标志 C++11。 谢谢!
【问题讨论】:
-
@ForEveR,您实际上是在实例化模板吗? (OP 应该包含完整的代码来重现错误,包括实例化)。