gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 23:44:16 45 4
gpt4 key购买 nike

在下面的 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 指令用于将名称引入当前作用域,否则不会。

例子:

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 不是类模板,您可以使用更简单的形式

using the_graph::node_list_iterator;

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

关于头文件中的 C++ 'using' 和 'using typename',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51883423/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com