gpt4 book ai didi

c++ - 存在冲突符号时使用一个或多个命名空间?

转载 作者:太空宇宙 更新时间:2023-11-04 11:48:10 25 4
gpt4 key购买 nike

使用两个命名空间的优缺点是什么

namespace library1
{
void function1();
}

namespace library1_sublibrary1
{
void function1();
}

代替

namespace library1
{
void function1();

namespace sublibrary1
{
void function1();
}
}

在第二种情况下我必须完全限定符号,对吗?是否有任何具体原因(超出个人品味)为什么一个人应该优先于另一个人?

最佳答案

重复两条评论。

在两个版本(一个:两个独立的命名空间,两个:嵌套的命名空间)之间,名称查找存在一些差异。其中大部分可以通过using-declaration(例如using outer::function0;)或using-directive(例如using namespace library1;),但有些不能。

1。内部命名空间内的非限定查找

#include <iostream>

namespace outer
{
void function0() { std::cout << "outer::function0" << std::endl; }

namespace inner
{
void test0()
{
function0(); // finds outer::function
}
}
}

namespace another_outer
{
void test0_1()
{
// either
using namespace outer;
// or
using outer::function0;

function0();
}
}

注意您还可以将 using-directiveusing-declaration 放在 another_outer 的命名空间范围内,但仍然存在一些区别:

2。停止不合格查找

一旦找到名称,非限定查找就会在范围内停止(然后不搜索外部范围)。这可用于隐藏其他作用域中的函数。这是与此相关的问题示例;另见 this answer .

void function1() { std::cout << "::function1" << std::endl; }

namespace another_outer
{
void function1() { std::cout << "another_outer::function1" << std::endl; }
}

namespace outer
{
namespace inner
{
void function1() { std::cout << "outer::inner::function1" << std::endl; }
}

void test1()
{
function1(); // finds ::function1

{
using namespace inner;
function1(); // finds (only) outer::inner::function1
}

{
using namespace another_outer;
//function1(); // finds both ::function1 and another_outer::function1
// error: ambiguous call
}
}
}

3。日常事件

这不是关于两个变体之间的区别,而是解决“在第二种情况下我必须完全限定符号,对吧?”。当您没有在函数调用中限定函数名称时,就会发生依赖于参数的查找。它在与参数关联的命名空间(和类)中查找函数的名称。

namespace outer
{
struct ADL {};
void function2(ADL&) { std::cout << "outer::function2" << std::endl; }

namespace inner
{
void function2(ADL const&);
void test2()
{
ADL x;
function2(x); // finds both outer::function2
// and outer::inner::function2
// overload resolution selects outer::function2
}
}
}

int main()
{
outer::ADL x;
function2(x); // finds outer::function2
outer::inner::test2();

// other tests:
outer::inner::test0();
outer::test1();
}

4。特别是关于冲突符号

如果在 outerouter::inner 中有两个相同的函数(返回类型除外),并且都在某个调用中找到,则该调用将是不明确的.但不合格的查找可能只会找到其中之一:

namespace outer
{
void function3() { std::cout << "outer::function3()" << std::endl; }

namespace inner
{
void function3()
{ std::cout << "outer::inner::function3()" << std::endl; }

void test3()
{
function3(); // only finds outer::inner::function3
}
}

void test3_1()
{
using namespace inner;
//function3(); // finds both outer::function3
// and outer::inner::function3
// error: ambiguous call

using inner::function3;
function3(); // finds (only) outer::inner::function3
}
}

namespace another_outer
{
void function3() { std::cout << "another_outer::function3" << std::endl; }

void test3_1()
{
using namespace outer;
function3(); // still only finds another_outer::function3

using outer::function3;
function3(); // only finds outer::function3
}
}

关于c++ - 存在冲突符号时使用一个或多个命名空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19294068/

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