【发布时间】:2012-05-25 07:47:04
【问题描述】:
在下面的设置中,我怎样才能使我可以在派生类Derived<T>中引用名称Bar?
template <typename T> struct Foo
{
template <typename U> struct Bar { };
};
template <typename T> struct Derived : Foo<T>
{
// what goes here?
Bar<int> x; // Error: 'Bar' does not name a type
};
我试过using Foo<T>::Bar;,但这没有帮助。是否有任何类型的 using 声明可以使派生类知道嵌套基模板的名称,以便我可以保留简单的声明 Bar<int> x?
我知道我可以说typename Foo<T>::template Bar<int> x;,但我有很多这样的情况,我不想用这么多冗长不必要地给代码增加负担。我也有很多不同的“ints”,所以每个嵌套模板实例的typedef 也不可行。
另外,我现在不能使用 GCC 4.7 也不能使用 C++11,因此我想要一个没有模板别名的“传统”解决方案。
【问题讨论】:
标签: c++ templates base-class name-lookup