Dan*_* G. 2 c++ namespaces vector reference-wrapper
我目前正在从事大型代码项目,并希望借此机会学习和使用名称空间。我定义的所有类都驻留在单个名称空间Test中。
我的一个类(在这里称为Thing)具有唯一的ID。我需要能够保存对某些事物的引用的std :: vector,因此我正在使用std :: reference_wrappers。在程序中的某些点,我需要从向量中删除某些std :: reference_wrappers,所以我使用std :: find:
#include <algorithm>
#include <functional>
#include <vector>
namespace Test {
class Thing {
private:
const int id;
public:
Thing(int id);
const int ID() const;
};
}
Test::Thing::Thing(int id) : id(id) { }
const int Test::Thing::ID() const {
return id;
}
inline bool operator==(const Test::Thing& lhs, const Test::Thing& rhs) {
return lhs.ID() == rhs.ID();
}
inline bool operator!=(const Test::Thing& lhs, const Test::Thing& rhs) {
return !(lhs == rhs);
}
int main() {
Test::Thing t1(5);
Test::Thing t2(7);
auto ref1 = std::ref(t1);
auto ref2 = std::ref(t2);
std::vector<std::reference_wrapper<Test::Thing>> v;
v.push_back(ref1);
v.push_back(ref2);
auto pos = std::find(v.begin(), v.end(), ref2);
}
Run Code Online (Sandbox Code Playgroud)
当我尝试对此进行编译时,出现错误:
error: no match for ‘operator==’ (operand types are ‘std::reference_wrapper<Test::Thing>’ and ‘const std::reference_wrapper<Test::Thing>’)
Run Code Online (Sandbox Code Playgroud)
但是,如果删除命名空间,代码将正确编译:
#include <functional>
#include <vector>
#include <algorithm>
class Thing {
private:
const int id;
public:
Thing(int id);
const int ID() const;
};
Thing::Thing(int id) : id(id) { }
const int Thing::ID() const {
return id;
}
inline bool operator==(const Thing& lhs, const Thing& rhs) {
return lhs.ID() == rhs.ID();
}
inline bool operator!=(const Thing& lhs, const Thing& rhs) {
return !(lhs == rhs);
}
int main() {
Thing t1(5);
Thing t2(7);
auto ref1 = std::ref(t1);
auto ref2 = std::ref(t2);
std::vector<std::reference_wrapper<Thing>> v;
v.push_back(ref1);
v.push_back(ref2);
auto pos = std::find(v.begin(), v.end(), ref2);
}
Run Code Online (Sandbox Code Playgroud)
正确的解决方案是将运算符移到命名空间,在该命名空间中,依赖于参数的查找(ADL)可以找到它们:
namespace Test {
class Thing {
private:
const int id;
public:
Thing(int id);
const int ID() const;
};
inline bool operator==(const Thing& lhs, const Thing& rhs) {
return lhs.ID() == rhs.ID();
}
inline bool operator!=(const Thing& lhs, const Thing& rhs) {
return !(lhs == rhs);
}
}
Test::Thing::Thing(int id) : id(id) { }
const int Test::Thing::ID() const {
return id;
}
Run Code Online (Sandbox Code Playgroud)
标准库已经对操作符(例如<<流插入)进行了相同的操作。