[英]C++ 'using' and 'using typename' in a header file
In the C++ code below, could somebody explain what each of these lines mean in the private
section? 在下面的C ++代码中,有人可以解释这些部分中的每行在
private
部分中的含义吗? I have tried looking it up, but I still cannot figure out what they do. 我曾尝试查找它,但仍然无法弄清它们的作用。
I understand that using
is the equivalent to typedef
in C. So: 我了解
using
等效于C中的typedef
。因此:
using the_graph = graph<T_node, T_edge1, T_allocator, T_size>;
Means that you use the_graph
. 表示您使用
the_graph
。
But, in this instance, why would you call the scope resolution operator on it? 但是,在这种情况下,为什么要在其上调用范围解析运算符?
I don't think that it is any of the 4 methods described here . 我认为这不是这里描述的4种方法中的任何一种。
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;
};
The using
directive is used to bring a name into the current scope that otherwise is not. using
指令用于将名称带入当前范围,否则不带。
Example: 例:
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.
}
When a name is dependent upon a template parameter, the standard requires that you use the elaborate form 当名称取决于模板参数时,标准要求您使用精心设计的格式
using typename the_graph::node_list_iterator;
After that line, you can use node_list_iterator
as a typename. 在该行之后,可以将
node_list_iterator
用作类型名。
Had the_graph
not been a class template, you could have used the simpler form 如果
the_graph
不是类模板,则可以使用更简单的形式
using the_graph::node_list_iterator;
Further reading: Where and why do I have to put the "template" and "typename" keywords? 进一步阅读: 为什么必须在何处以及为什么要放置“ template”和“ typename”关键字?
Those using
make visible the associated name and avoid ambiguity. 那些
using
使人们看到了相关的名称,避免歧义。
Both bases should have type node_list_iterator
and method(s) node_begin
. 这两个库都应具有类型
node_list_iterator
和方法node_begin
。
Inside the definition and the declaration of a template, the keyword typename
is used to specify that the qualified-id (ie some_name::some_member
) is a type and not a variable. 在模板的定义和声明中,关键字
typename
用于指定合格ID(即some_name::some_member
)是类型而不是变量。 This keyword is necessary before template instantiation, because the compiler does not know the definition of some_name
, and without typename
it would supposes that some_member
is a variable (or function). 在模板实例化之前,此关键字是必需的,因为编译器不知道
some_name
的定义,并且如果没有typename
,它将假定some_member
是变量(或函数)。
Then the using directive using typename::node_list_iterator
make node_list_iterator
privately accessible from the graph
class. 然后,使用
using typename::node_list_iterator
的using指令可从graph
类私有访问node_list_iterator
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.