gpt4 book ai didi

c++ - 删除 "using namespace std::rel_ops"可以改变行为吗?

转载 作者:行者123 更新时间:2023-12-03 11:51:08 27 4
gpt4 key购买 nike

我最近发现一个大项目在一个经常包含的头文件和全局命名空间中有一个“using namespace std::rel_ops;”。哎哟。

具体来说,它导致了一个问题,因为这两个函数模板声明不明确:

namespace std::rel_ops {
template <class T>
bool operator!=(const T&, const T&);
}
namespace std {
template <class... TTypes, class... UTypes>
constexpr bool operator!=(const tuple<TTypes...>&, const tuple<UTypes...>&);
}

所以我尝试使用类似于 std::tie(a.m1, a.m2, a.m3) != std::tie(b.m1, b.m2, b.m3) 的表达式是错误的。

所以计划是删除 using namespace std::rel_ops; ,然后修复由此产生的编译器错误,可能是通过定义更具体的比较运算符函数。但我还想通过评估是否有可能改变这个大型项目中隐藏在其他地方的一些代码的含义,而不会导致编译器错误。

在什么情况下,如果有的话,两个带有和不带有指令 using namespace std::rel_ops; 的 C++ 程序在行为上会有所不同,因为它们都不是病态的,需要诊断?

我怀疑它需要另一个比较运算符函数模板,它不像 std::rel_ops 中的那样专门化,并且具有与 std::rel_ops 定义不同的有效行为。这两种情况在实际项目中似乎不太可能,更不可能一起考虑。

最佳答案

你有一个这样的例子,它讲述了关于这类错误的更多信息。根据 using namespace 程序返回不同的值。


#include <utility>

class Type {
public:
Type(int) {}
};

bool operator==(Type, Type) { return false; }

template<class T , class U>
bool operator!=(const T& lhs, const U& rhs) {
return lhs == rhs;
}

using namespace std::rel_ops;

int main() {
Type bar(1);
Type baz(1);
return bar != baz;
}

Live example

关于c++ - 删除 "using namespace std::rel_ops"可以改变行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60850307/

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