也许我的 Google 功能还不够强大。
使用 GCC 4.4.3,我有一组这样的类:
template <typename storage_t, typename index_t = std::size_t, typename
leaf_payload_t = std::size_t>
struct btree_node {
public:
typedef btree_node<storage_t, index_t, leaf_payload_t> this_t;
typedef boost::shared_ptr<this_t> handle_t;
// [...]
};
template <typename storage_t, typename index_t = std::size_t, typename
leaf_payload_t = std::size_t>
class btree {
public:
class caching_storage_t;
typedef btree_node<caching_storage_t, index_t, leaf_payload_t> node_t;
typedef typename node_t::handle_t nodehandle_t;
// [...]
class caching_storage_t {
public:
//typedef typename btree::nodehandle_t nodehandle_t; // Fails -- why?
typedef typename boost::shared_ptr<node_t> nodehandle_t;
// [...]
};
};
如您所见,我不得不在 caching_storage_t
中重新定义 nodehandle_t
,因为如果我尝试使用注释掉的 typedef 行(我更喜欢),我得到一个错误“no type named 'handle_t' in 'struct btree_node<...>'”——这显然是不正确的,编译器知道它,因为 typedef 在 btree
中工作正常.我还尝试了 using typename btree::nodehandle_t;
,我能想到的所有变体都无济于事。
这是语言/语法问题(如果是,正确的语法是什么),还是编译器错误?
(有一个类似的问题 here ,但它似乎并不适用,因为我正在尝试 typedef
的东西本身并不是一个模板。我没有别的办法找到似乎更接近。)
最佳答案
在我看来这像是一个编译器错误。可以肯定的是,我将您的代码放入 clang
并实例化 btree<int>::caching_storage_t
,所有工作正常也删除了注释字符。
它也适用于 GCC4.5.1 and GCC4.3.4 .
关于c++ - 如何对通过几层模板派生的类型进行typedef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3900890/