模板重载运算符不明确
Templated overloaded operator is ambiguous
对于许多其他容器、表达式模板和文字类型U
,我有一个容器template <typename T> class A
,其operator+
需要重载。
我目前的策略是定义一个模板函数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);
}
如何最好地解决歧义?
您需要提供一个重载模板,该模板在其不明确的参数上比两者都好:
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) -> ...;
相关文章:
- 在 C++17 中的命名空间和子命名空间中重载运算符是不明确的
- 如何解决不明确的运算符过载问题?
- 删除全局隐式函数 - 避免使用不明确的运算符
- C++打印模板容器错误(错误:"运算符<<"的不明确重载)理解?
- 运算符[]和运算符int()的不明确重载
- 为什么这个涉及重载运算符和隐式转换的C++表达式是不明确的
- 不明确的错误重载运算符<<QdataStream 子类和个人类
- 模板转换运算符的分辨率不明确
- msvc /permissive- std::string 重载运算符 '=' 不明确
- 提升错误:"运算符=="的重载不明确
- Rcpp/C++/R:比较日期时间矢量与日期时间时出错(错误:"运算符>"的不明确重载)
- 类运算符的不明确重载>>
- 错误:在为 i386 构建时,使用重载运算符'[]'不明确
- 重载运算符'<<'的使用不明确(操作数类型'ostream'(也称为"basic_ostream<char>")和"Person")
- "authlog = myfile"中"运算符 ="的不明确重载
- Clang 中的运算符重载不明确
- C++表达式模板不明确的运算符重载
- 运算符'*'不明确 (C++)
- Mongodb中的不明确<<运算符C++驱动程序文档生成器
- C++模板重载的不明确运算符