我有这样的事情:
#include <iostream>
namespace N
{
typedef std::pair<int, double> MyPair;
std::ostream& operator << (std::ostream& o, MyPair const & mypair)
{
///
}
}
int main()
{
N::MyPair pr;
std::cout << pr;
}
Run Code Online (Sandbox Code Playgroud)
这自然不起作用,因为ADL找不到operator<<因为namespace N与MyPair(不幸)相关联.Afaik可能不会添加到命名空间std,所以如果我选择operator <<在std中定义那将是非法的.那么......在这种情况下该怎么办?我不想明确限定operator <<,也不想写using namespace N.所以,问题是:
这是一些显示奇怪的代码:
namespace ns
{
typedef int int_type;
class classus {
public:
int a = 0;
};
void print(classus c){
printf("a equals %i \n", c.a);
}
void print(int_type a){
printf("a equals %i \n", a);
}
}
int main(int argc, char* argv[])
{
ns::int_type a1 = 2;
ns::classus a2;
ns::print(a1); // this line wont work without explicit ns:: scope
print(a2); // this line works with or without explicit ns:: scope
}
Run Code Online (Sandbox Code Playgroud)
它在Visual Studio 2017上构建并运行。Intellisense也对此感到非常满意。
似乎包含在名称空间中的class变量污染了该名称空间范围的整个行。这是错误还是功能?不管哪种方式,是否有关于此的一些文档...没有找到任何