我的代码可以用 VC9(Microsoft Visual C++ 2008 SP1)编译,但不能用 GCC 4.2(在 Mac 上,如果重要的话)编译。如果我堆积足够多的限定符和关键字,我可以强制它在 GCC 中工作,但这似乎不对。
这是展示我的问题的最小代码示例:
template< typename N >
struct B {
typedef N n_type; // can derived class access typedef?
void foo() {} // can derived class access function?
};
template< typename N >
struct D : public B<N> {
typedef B<N> b_type;
typedef typename b_type::n_type bn_type;
void f1( n_type ) {} // ERROR: 'n_type' has not been
// declared
void f2( typename B<N>::n_type ) {} // OK, verbose
void f3( b_type::n_type ) {} // ERROR: 'struct B<N>::n_type' is
// not a type
void f4( typename b_type::n_type ) {} // OK, verbose
void f5( bn_type ) {} // OK, verbose typedefs
void f6() { foo(); } // ERROR: there are no arguments to
// 'foo' that depend on a template
// parameter, so a declaration of
// 'foo' must be available
void f7() { b_type::foo(); } // OK, verbose
};
我期望派生自另一个模板类的模板类能够直接使用继承的 typedef 和函数,我错了吗?有没有比我目前提出的更好的方法来做到这一点?
最佳答案
Am I wrong to expect a template class derived from another template class to be able to use inherited typedefs and functions directly?
是的,这通常不会像您预期的那样工作。 C++ 名称查找规则指定名称仅在模板化基类中搜索,如果它依赖于模板参数(如果它是“依赖名称”)。如果名称不依赖于模板参数,则不会在那里进行搜索。 (另见 this C++ FAQ Lite entry)
要从依赖基类调用函数,最简单的方法是使用 this->
,因为 this
总是隐含地依赖名称:
void f6() { this->foo(); }
关于c++ - 模板基类类型定义和函数的更好 C++ 语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2095223/