【发布时间】:2012-08-22 10:38:52
【问题描述】:
我有一个容器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);
}
如何最好地解决歧义?
【问题讨论】:
标签: c++ templates c++11 operator-overloading