相关疑难解决方法(0)

使用另一个命名空间中的typedef进行ADL

我有这样的事情:

#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 NMyPair(不幸)相关联.Afaik可能不会添加到命名空间std,所以如果我选择operator <<在std中定义那将是非法的.那么......在这种情况下该怎么办?我不想明确限定operator <<,也不想写using namespace N.所以,问题是:

  1. 如何重构代码?
  2. 为什么ADL不会关联typedef的名称空间?严重的原因?这会很好,例如在这种情况下.谢谢

c++ typedef argument-dependent-lookup

15
推荐指数
1
解决办法
1596
查看次数

C ++名称空间的奇怪行为

这是一些显示奇怪的代码:

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变量污染了该名称空间范围的整个行。这是错误还是功能?不管哪种方式,是否有关于此的一些文档...没有找到任何

c++ namespaces visual-c++

8
推荐指数
1
解决办法
184
查看次数