gpt4 book ai didi

c++ - 函数重载失败 : why did these operators clash?

转载 作者:太空宇宙 更新时间:2023-11-04 12:28:57 34 4
gpt4 key购买 nike

我有一个很大的代码库,其中包括两个主要的命名空间:引擎和应用程序。

引擎将一个 vector3 类定义为另一个 vector3 类的 typedef,具有位于引擎命名空间而不是 vector3 类中的相等运算符。我向应用程序添加了一个类,该类在应用程序命名空间中也有相等运算符。

当我尝试编译时,不相关但接近的 vector3 比较失败了,因为它找不到合适的相等运算符。我怀疑我造成了冲突,因此将我的相等运算符移到我添加的类中,并且编译成功。

// engine.h
namespace Engine
{
class Vector3Impl { ... };
typedef Vector3Impl Vector3;
bool operator==(Vector3 const &lhs, Vector3 const &rhs) { ... }
}


// myfile.cpp
#include "engine.h"

namespace application
{
class MyClass { ... };
bool operator==(MyClass const &lhs, MyClass const &rhs) { ... }

void myFunc(...)
{
if ( myClassA == myClassB ) { ... } // builds
}

void anotherFunc(...)
{
Engine::Vector3 a, b;
...
if ( a == b ) { ... } // fails
}
}

但是仔细想想我不明白编译失败的原因。没有从 vector3 到我的类的隐式转换,反之亦然,并且依赖于参数的查找应该从引擎命名空间中提取相等运算符并匹配它。

我已尝试在示例 C++ 项目中重现此错误,但无法解决。大型代码库中一定有什么东西导致了这个问题,但我不确定从哪里开始寻找。类似于流氓“使用引擎”的对立面?有人有什么想法吗?

最佳答案

C++ 标准,3.4.4.2 声明:

For each argument type T in the function call, there is a set of zero or more associated namespaces and a set of zero or more associated classes to be considered. The sets of namespaces and classes is determined entirely by the types of the function arguments (and the namespace of any template template argument). Typedef names and using-declarations used to specify the types do not contribute to this set.

ADL 不适用于 typedef。

关于c++ - 函数重载失败 : why did these operators clash?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/211376/

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