2012-08-22 38 views
2

我有一個容器template <typename T> class A其中operator+需要重載許多其他容器,表達式模板和文字類型U模板化的重載運算符不明確

我目前的策略是定義一個模板函數wrap封裝元素如何獨立進行訪問,並定義

template <typename T, typename U> 
auto operator+(Wrapped<T>&& lhs, Wrapped<U>&& rhs) -> decltype(...) 
{ 
    .... 
} 

以下重載operator+,然而,是模糊的:

template <typename T, typename U> 
auto operator+(const A<T>& lhs, U&& rhs) -> 
    decltype(wrap(lhs) + wrap(std::forward<U>(rhs))) 
{ 
    return wrap(lhs) + wrap(std::forward<U>(rhs)); 
} 

template <typename T, typename U> 
auto operator+(T&& lhs, const A<U>& rhs) -> 
    decltype(wrap(std::forward<T>(lhs)) + wrap(rhs)) 
{ 
    return wrap(std::forward<T>(lhs)) + wrap(rhs); 
} 

我怎樣才能解決最好的歧義?

回答

2

您需要提供比都在爭論更好它是曖昧過載模板:我想在同一行

template<typename T> 
auto operator+(const A<T> &lhs, const A<T> &rhs) -> ...; 

template<typename T, typename U> 
auto operator+(const A<T> &lhs, const A<U> &rhs) -> ...; 
+0

。不過,我仍然會遇到過度的歧義。 – jk4736

+0

@ jk4736如果你發佈更多的代碼,它可能會告訴如何解決歧義。這個呼籲有什麼爭議含糊不清? – ecatmur

+0

哦!它可能是模棱兩可的,因爲operator +被稱爲右值A ?很可能是 – jk4736