[英]Use of overloaded operator '<<' is ambiguous
我正在编写一个类float32x4_t
来模拟 x86 平台上的 ARM NEON 数据类型。 它的对象中有 4 个元素。
我想cout
的float32x4_t
实例(打印出逗号分隔4种元素),但我的重载函数无法编译。
我的代码是:
#include <iostream>
struct float32x4_t
{
float val[4];
float& operator [] (int i) {
return val[i];
}
const float& operator [] (int i) const {
return val[i];
}
int size() const {
return 4;
}
};
template<class T>
std::ostream & operator << (std::ostream & os, const T& v)
{
for (int i=0; i<v.size(); i++) {
os << v.val[i] << ", ";
}
os << std::endl;
return os;
}
/// @ingroup add
/// r[i] = a[i] + b[i]
static inline float32x4_t vaddq_f32(float32x4_t a, float32x4_t b)
{
float32x4_t r;
for (int i=0; i<4; i++) {
r[i] = a[i] + b[i];
}
return r;
}
#include <stdio.h>
int main(int argc, char** argv)
{
float32x4_t v1 = { 1.0, 2.0, 3.0, 4.0 };
float32x4_t v2 = { 1.0, 1.0, 1.0, 1.0 };
float32x4_t sum = vaddq_f32(v1, v2);
std::cout << sum << std::endl;
return 0;
}
整个错误是这样的:
/home/zz/work/neon_pedal/neon_pedal.h:25:24: error: use of overloaded operator '<<' is ambiguous (with operand types 'std::basic_ostream<char>::__ostream_type' (aka 'basic_ostream<char>') and 'const char [3]')
os << v.val[i] << ", ";
~~~~~~~~~~~~~~ ^ ~~~~
/home/zz/work/neon_pedal/main.cpp:18:15: note: in instantiation of function template specialization 'operator<<<float32x4_t>' requested here
std::cout << sum << std::endl;
^
/usr/lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/ostream:565:5: note: candidate function [with _Traits = std::char_traits<char>]
operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
^
/usr/lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/ostream:548:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>]
operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
^
/usr/lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/ostream.tcc:321:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>]
operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
^
/home/zz/work/neon_pedal/neon_pedal.h:22:16: note: candidate function [with T = char [3]]
std::ostream & operator << (std::ostream & os, const T& v)
如何解决此编译错误?
注意:这里重载的operator <<
与模板一起使用,这是因为将不仅仅是float32x4_t
类型。 会有诸如float32x2_t
、 int8x8_t
、 int16x8_t
等。
您的模板operator <<
试图为所有内容提供重载; 不仅仅是您的类型(因此意外匹配是模棱两可的)。
有很多方法可以解决这个问题,但是由于您已经定义了自己的类型,并且整个目标是最小化您必须编写以匹配的operator <<
重载的数量,请考虑通用 TxN 模板,编写一个(单数)重载,然后将您的真实类型别名为自然进化。
#include <iostream>
#include <array>
template<class T, size_t N = 4>
struct TxN
{
T val[N];
TxN() = default;
T& operator[](size_t i)
{
return val[i];
}
T const& operator[](size_t i) const
{
return val[i];
}
size_t size() const
{
return N;
}
// there are at least three different ways to do this operator, but
// this is the easiest, so I included below.
friend std::ostream& operator <<(std::ostream& os, TxN<T,N> const& t)
{
os << t.val[0];
for (size_t i=1; i<N; ++i)
os << ',' << t.val[i];
return os;
}
};
// now creating TypeN shrouds is trivial.
using float32x4_t = TxN<float, 4>;
using int16x8_t = TxN<short,8>;
/// @ingroup add
/// r[i] = a[i] + b[i]
static inline float32x4_t vaddq_f32(float32x4_t a, float32x4_t b)
{
float32x4_t r;
for (int i=0; i<4; i++) {
r[i] = a[i] + b[i];
}
return r;
}
int main(int argc, char** argv)
{
float32x4_t v1 = { 1.0, 2.0, 3.0, 4.0 };
float32x4_t v2 = { 1.0, 1.0, 1.0, 1.0 };
float32x4_t sum = vaddq_f32(v1, v2);
std::cout << sum << '\n';
int16x8_t i0 = { 1,2,3,4,5,6,7,8 };
std::cout << i0 << '\n';
return 0;
}
输出
2,3,4,5
1,2,3,4,5,6,7,8
您正在尝试为所有类型重载<<
运算符。 因此,如果有一种类型的<<
运算符已经在 std 命名空间中重载,它将是模棱两可的,因为编译器不知道选择哪一个,是你的重载还是 std 命名空间中的重载。
for (int i=0; i<v.size(); i++) {
os << v.val[i] << ", ";
}
具体来说, const char[] = ", "
导致了这个问题,因为 std 命名空间中已经存在重载。 请注意,您也在调用os << std::endl
但您使用std::
是明确的,因此没有歧义。 如果您想使用std
命名空间中存在的ostream
重载(我假设您这样做),则无法使您的模板化重载可行。
你可以在你的结构中声明一个朋友重载。
friend std::ostream & operator << (std::ostream & os,const float32x4_t & self){
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.