English 中文(简体)
图一
原标题:Output a C array through ostream

I m 试图利用流输出C阵列。

For array of ints, I wrote the code like this

template <size_t N>
ostream& operator<< (ostream& os, const int (&x)[N])
{
    for(int i=0; i<N; i++)
        os<<x[i]<<",";
    return os;
}
int main()
{
    int arr[]={1,2,3};
    cout<<arr<<endl;
    return 0;
}

此外,它还从事了相当的罚款。

然后,我将其概括为更多的类型(如果园、浮体等),因此,我更新了原版本如下:

template <class T, size_t N>
ostream& operator<< (ostream& os, const T (&x)[N])
{
    for(int i=0; i<N; i++)
        os<<x[i]<<",";
    return os;
}

the main function didn t change, However, this time, when I compile it, error occured.

In function `std::ostream& operator<<(std::ostream&, const T (&)[N]) [with T = int, long unsigned int N = 3ul] :
a.cpp:15:   instantiated from here
a.cpp:9: error: ambiguous overload for `operator<<  in `(+os)->std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>]((*((+(((long unsigned int)i) * 4ul)) + ((const int*)x)))) << "," 
/usr/lib/gcc/x86_64-redhat-linux/3.4.5/../../../../include/c++/3.4.5/bits/ostream.tcc:121: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>] <near match>
/usr/lib/gcc/x86_64-redhat-linux/3.4.5/../../../../include/c++/3.4.5/bits/ostream.tcc:155: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>] <near match>
/usr/lib/gcc/x86_64-redhat-linux/3.4.5/../../../../include/c++/3.4.5/bits/ostream.tcc:98: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]

我如何确定这一点? 感谢你提出任何建议。

最佳回答

There already exists an overload for operator << (const char*) which is ambiguous with your template.

您可限制与SFINAE的模板,以排除char:

template <class T, size_t N,
     typename = typename std::enable_if<!std::is_same<char, T>::value>::type>
ostream& operator<< (ostream& os, const T (&x)[N])
问题回答

编辑实际上抱怨<代码>”、“。 如果你去掉,你就会看到它会做罚款。

template <class T, size_t N>
ostream& operator<< (ostream& os, const T (&x)[N])
{
    for(size_t i = 0; i < N; i++)
        os << x[i];
    return os;
}
// Output: 123

缩略语的类型是一系列<代码>。 N const char, but it decays to a const char*,在os << x[i] << “, calls.

This is actually a nice example: at the point of call, the array hasn t decayed yet, so the array length is preserved and can be passed as a template parameter. Very clever!

可在现代C++中使用“ o”器和复印算法实施:

template<class T, size_t N,
  typename = typename std::enable_if<!std::is_same<char, T>::value>::type>
std::ostream& operator<<(std::ostream& os, const T (&arr)[N]) {
   std::copy_n(arr, N, std::ostream_iterator<T>(os, ", "));
   return os;
}

产出<代码>1、2、3、。

(I tested this with gcc 11.4.0 using the default C++17.)

问题在于:operator<< 已经在包括const char*在内的许多歌剧上超载。 您的超载吨位比const char*超载。

The recommended and usual way to solve this is to put your overload into its own namespace so that ADL can be used.

namespace detail
 {
   template <class T, size_t N> std::ostream& operator<< (std::ostream& os, const T (&x)[N])
   {
       for(int i=0; i<N; i++)
        os<<x[i]<<",";
     return os; 
   }
}

https://godbolt.org/z/f6K7W4sfG”rel=“nofollow noreferer”> Working demo





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签