c++ - 如何对通过几层模板派生的类型进行typedef?

标签 c++ templates typedef

也许我的 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/

相关文章:

c# - 如何附加调试器以从托管(C#)包装器进入 native (C++)代码?

c++ - 打印二进制递归算法

c++ - 我应该使用什么来代替 sscanf?

c++ - 为 std::vector<double> boost 自定义验证器

c++ - 尝试与 typedef 交 friend 时出现 "elaborated type refers to typedef"错误

C:修改通过 typedef 传递的 int 指针的最佳方法是什么?

c++ - 将模板参数包存储为非类模板的属性

c++ - 为什么类 B<T> 看不到父类 A<T> 的 protected 成员?

c++ - 为什么重载运算符&&错误?

c++ - 需要帮助解码这个 typedef