头文件中的 C++ 'using' 和 'using typename'

C++ 'using' and 'using typename' in a header file

在下面的 C++ 代码中,有人可以解释一下 private 部分中每一行的含义吗?我已经尝试查找它,但我仍然无法弄清楚它们的作用。

我理解 using 等同于 C 中的 typedef。所以:

using the_graph = graph<T_node, T_edge1, T_allocator, T_size>;

表示您使用the_graph

但是,在这种情况下,您为什么要对其调用范围解析运算符?

我不认为这是 4 种方法中的任何一种 described here

template <class T_node, class T_edge1, class T_edge2, class T_allocator, class T_size = uint32_t>
class graph : private virtual graph<T_node, T_edge1, T_allocator, T_size>, private virtual graph<T_node, T_edge2, T_allocator, T_size>
{

public:

    using the_graph = graph<T_node, T_edge1, T_allocator, T_size>;


private:

    using typename the_graph::node_list_iterator;
    using the_graph::node_begin;
};

那些 using 使关联的名称可见并避免歧义。

两个碱基都应具有类型 node_list_iterator 和方法 node_begin

在模板的定义和声明中,关键字typename用于指定qualified-id(即some_name::some_member)是类型而不是变量。这个关键字在模板实例化之前是必需的,因为编译器不知道 some_name 的定义,如果没有 typename 它会假设 some_member 是一个变量(或函数)。

然后使用指令 using typename::node_list_iterator 使 node_list_iterator 可以从 graph class.

私有访问

using 指令用于将名称引入当前作用域,否则不会。

示例:

struct Foo
{
    struct Bar {};
};

int main()
{
   Bar b1; // Not ok. Bar is not in scope yet.

   using Foo::Bar;
   Bar b2; // Ok. Bar is now in scope.
}

当名称依赖于模板参数时,标准要求您使用详尽的形式

using typename the_graph::node_list_iterator;

在那行之后,您可以使用 node_list_iterator 作为类型名称。

如果 the_graph 不是 class 模板,您可以使用更简单的形式

using the_graph::node_list_iterator;

进一步阅读:Where and why do I have to put the "template" and "typename" keywords?