0

有人可能問,但我感興趣的是如何使這個代碼工作,2012 VC: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一起呢?

+0

實際上它看起來像MSVC的bug,因爲g ++和clang ++根本不調用ns :: swap。我會嘗試發佈另一個可能的解決方案 –

回答

0

好吧,這裏是性病的解決方案::向量()掉期(五):

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) { } 
} 
+0

看來g ++和clang ++永遠不會調用ns :: swap .... –

0
#ifdef NS_USE_STL 

#include <utility> 

namespace ns { 

using std::swap; 

} 

#else 

// define your own swap 

#endif