【发布时间】:2018-10-19 19:28:31
【问题描述】:
在以下场景中,在 function() 内部,ss << bb 行出现错误:
binary '
我的 ADL 理解是它将查看当前命名空间(即 AppNamespace::InnerNamespace),由于没有找到 operator <<,它会查看参数的命名空间,因为 CommonType 在全局命名空间中我希望能够找到 CommonTypes.h 中定义的operator <<。
显然我的理解是错误的。谁能弄清楚这应该如何工作?
Main.cpp
namespace AppNamespace
{
typedef std::vector<std::string> OtherType;
std::ostream& operator << (std::ostream& os, const OtherType& ot)
{
for (auto& el : ot)
{
os << el;
}
return os;
}
namespace InnerNamespace
{
void function()
{
CommonType bb;
std::stringstream ss;
ss << bb;
}
}
}
int main()
{
AppNamespace::InnerNamespace::function();
return 0;
}
CommonTypes.h:
#pragma once
#include <vector>
typedef std::vector<uint8_t> CommonType;
std::ostream& operator << (std::ostream& os, const CommonType& bb)
{
for (auto& el : bb)
{
os << el;
}
return os;
}
【问题讨论】:
-
即使没有别名问题,你对ADL的理解也是错误的。参数命名空间的重载被认为是甚至存在于全局命名空间中。我们首先构建要考虑的命名空间集。
标签: c++ namespaces operator-overloading argument-dependent-lookup