C++ typedef typename classname::template

C++ typedef typename classname::template

本文关键字:template classname typename typedef C++      更新时间:2023-10-16

我无法解析下面这行代码的含义:

typedef typename Allocator::template rebind<Mapped>::other mapped_type_allocator;

这是分配器重新绑定的代码(第63行)https://gcc.gnu.org/onlinedocs/libstdc + +/libstdc + + api - 4.5/a00756_source.html)

这和下面的有什么不同?

typedef typename Allocator::rebind<Mapped>::other mapped_type_allocator;
typedef typename Allocator::template rebind<Mapped>::other mapped_type_allocator;

这是一个模板化的类型定义——它将mapped_type_allocator建立为模板的别名。


typedef typename Allocator::rebind<Mapped>::other mapped_type_allocator;

这是一个类型的typedef。要编译成功,需要定义/知道Mapped


Allocator::rebind<typename X>::other(作为一个概念)被期望定义一个模板,而不是一个类型

这里我展示了这个声明在不同行的分组:

typedef                                                    mapped_type_allocator;
        typename Allocator::                       ::other 
                            template rebind<Mapped>

关键字typenametemplate后面有空格,这可能会使您感到困惑。关于使用这两个关键字的原因,请参阅我必须在哪里以及为什么要放置"模板"。和";typename"关键字?.