如何过载操作符<对于C ++中的数组? [英] how overload operator<< for an array in C++?

查看:109
本文介绍了如何过载操作符<对于C ++中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图这样做:

template <typename T>
ostream &operator<<(ostream &os, T &arr)
{ /*...*/ }

但是 T 代表一个数组?为数组重载< 运算符是否正确?

But can T represent an array? Is it correct to overload the << operator for an array?

编辑:

根据Kerrek SB的建议,这里是< / code>:

According to Kerrek SB's advice, here is my implementation for <<:

template <typename T, unsigned int N>
ostream &operator<<(ostream &os, const T (&arr)[N])
{
    int i;
    for(i = 0; i < N; i++)
        os << arr[i] << " ";
    os << endl;
    return os;
}

我的实现是否正确?我遇到了编译错误。

Is my implementation right? I got a compilation error.

推荐答案

您可以这样做:

template <typename T, unsigned int N>
std::ostream & operator<<(std::ostream & os, const T (&arr)[N])
{
  // ..
  return os;
}

这当然只适用于编译时数组。请注意, T 是内置类型或 std 中的类型时,不允许实例化此模板,命名空间!

This works only for compile-time arrays, of course. Note that you are not allowed to instantiate this template when T is a built-in type or a type in the std namespace!

如果可能的话,最好尽可能使这个内联,因为你会为每个 N 。 (漂亮的打印机有此示例。)

Probably best to make this inline if possible, since you'll cause a separate instantiation for every N. (The pretty printer has an example of this.)

你会注意到,毯子模板引入了歧义,因为 os<< Hello现在有两个可能的重载:模板匹配 const char(&)[6] 对于衰减到指针 const char * ,它们都具有相同的转换序列。我们可以通过禁用字符数组的重载来解决这个问题:

You will notice, though, that the blanket template introduces an ambiguity, because os << "Hello" now has two possible overloads: the template matching const char (&)[6], and the (non-template) overload for the decay-to-pointer const char *, which both have identical conversion sequences. We can resolve this by disabling our overload for char arrays:

#include <ostream>
#include <type_traits>

template <typename T, unsigned int N>
typename std::enable_if<!std::is_same<T, char>::value, std::ostream &>::type
operator<<(std::ostream & os, const T (&arr)[N])
{
  // ..
  return os;
}

事实上,为了更加通用,你还可以使 basic_ostream 参数模板参数:

In fact, to be even more general you can also make the basic_ostream parameters template parameters:

template <typename T, unsigned int N, typename CTy, typename CTr>
typename std::enable_if<!std::is_same<T, char>::value,
                        std::basic_ostream<CTy, CTr> &>::type
operator<<(std::basic_ostream<CTy, CTr> & os, const T (&arr)[N])
{
  // ..
  return os;
}

鉴于 T 必须是用户定义的类型,您甚至可以用 is_fundamental< T> is_same $ c>以获得更多的检查(但用户仍然不能使用这个标准库类型的数组)。

In view of the fact that T must be a user-defined type, you could even replace is_same<T, char> with is_fundamental<T> to get a bit more checking (but users still must not use this for arrays of standard library types).

这篇关于如何过载操作符&lt;对于C ++中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆