【问题标题】:VC++ 2012: "found using argument-dependent lookup" work-around?VC++ 2012:“使用参数相关查找找到”解决方法?
【发布时间】:2014-03-27 20:06:51
【问题描述】:

可能有人问过,但我对如何使这段代码与 VC 2012 一起工作很感兴趣:

#include <vector>

namespace ns {
   struct Obj { };
   template <class T> void swap(T& a, T& b) { }
}


int main(int argc, char* argv[])
{
   std::vector<ns::Obj> v;
   std::vector<ns::Obj>().swap(v); <-'std::swap' : ambiguous call to overloaded function
   return 0;
}

我必须使用这个 ns 命名空间,并且不能更改它。它必须能够在没有 STL 的情况下工作,并且有自己的 swap()。但是,如何正确地将它与 STL 一起使用?

【问题讨论】:

  • 实际上它看起来像MSVC的一个错误,因为g++和clang++根本不调用ns::swap。我会尝试发布另一个可能的解决方案。

标签: c++ visual-studio-2012 overloading ambiguous argument-dependent-lookup


【解决方案1】:

好的,这是 std::vector().swap(v) 的解决方案:

namespace ns {
inline void swap(Obj*& a, Obj*& b) { }
inline void swap(Obj& a, Obj& b) { }
}

但是,这不是一般的修复,因为我可以有这样的东西:

std::map<int, std::map<int, ns::Obj>> m;
m[0] = m[1];

编辑:

这是在上述情况下有效的另一个技巧

namespace ns {
template <class T> void swap(T* a, T* b) { }
inline void swap(Obj& a, Obj& b) { }
}

【讨论】:

    【解决方案2】:
    #ifdef NS_USE_STL
    
    #include <utility>
    
    namespace ns {
    
    using std::swap;
    
    }
    
    #else
    
    // define your own swap
    
    #endif
    

    【讨论】:

      猜你喜欢
      • 2014-01-03
      • 1970-01-01
      • 1970-01-01
      • 2011-03-14
      • 2018-01-08
      • 1970-01-01
      • 1970-01-01
      • 2017-05-30
      • 2023-01-23
      相关资源
      最近更新 更多