C++流运算符的部分模板特化

发布于 2024-12-11 12:03:24 字数 534 浏览 0 评论 0 原文

我有一个带有友元函数的 Matrix 类,可与运算符 << 一起使用。这一切都工作正常,但我现在想要部分专门化该友元函数,以便在 Matrix 类将 Matrix 作为其模板参数时以不同的方式工作(即,当该类的实例已声明为 Matrix> 时)。首先在类定义中我

template <typename U>
friend std::ostream& operator<<(std::ostream& output, const Matrix<U>& other);

尝试添加,

friend std::ostream& operator<<(std::ostream& output, const Matrix<Matrix<char> >& other);

但这给了我来自编译器的多个声明错误。 我似乎不知道如何实现这一点。

I have a Matrix class with a friend function to use with operator<<. This all works fine but I now want to partially specialize that friend function to work differently if the Matrix class has Matrix as its template parameter (i.e. when the instance of the class has been declared like Matrix< Matrix< char > >). In the class definition first I had

template <typename U>
friend std::ostream& operator<<(std::ostream& output, const Matrix<U>& other);

and I tried adding

friend std::ostream& operator<<(std::ostream& output, const Matrix<Matrix<char> >& other);

but this gave me multiple declaration errors from the compiler.
I can't seem to figure out how to accomplish this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〆一缕阳光ご 2024-12-18 12:03:24

不存在函数模板的部分特化

你需要重载,而不是专业化。这应该可以干净地编译、链接和运行(对我来说就是如此):

#include <iostream>

template <typename T>
class Matrix {
  public:
    template <typename U> friend std::ostream& 
        operator<<(std::ostream& output, const Matrix<U>& other);
    friend std::ostream& 
        operator<<(std::ostream& output, const Matrix<Matrix<char> >& other);    
};


template <typename U>
std::ostream& 
operator<<(std::ostream& output, const Matrix<U>& other)
{
    output << "generic\n";
    return output;
}

std::ostream& 
operator<<(std::ostream& output, const Matrix<Matrix<char> >& other)
{
    output << "overloaded\n";
    return output;
}

int main ()
{
    Matrix<int> a;
    std::cout << a;

    Matrix<Matrix<char> > b;
    std::cout << b;
}

如果您从中收到编译器错误,则您可能有一个有错误的编译器。

There's no such thing as a partial specialization of a function template.

You need overloading, not specialization. This should compile, link, and run cleanly (it does for me):

#include <iostream>

template <typename T>
class Matrix {
  public:
    template <typename U> friend std::ostream& 
        operator<<(std::ostream& output, const Matrix<U>& other);
    friend std::ostream& 
        operator<<(std::ostream& output, const Matrix<Matrix<char> >& other);    
};


template <typename U>
std::ostream& 
operator<<(std::ostream& output, const Matrix<U>& other)
{
    output << "generic\n";
    return output;
}

std::ostream& 
operator<<(std::ostream& output, const Matrix<Matrix<char> >& other)
{
    output << "overloaded\n";
    return output;
}

int main ()
{
    Matrix<int> a;
    std::cout << a;

    Matrix<Matrix<char> > b;
    std::cout << b;
}

If you are getting compiler errors from this, you probably have a buggy compiler.

强者自强 2024-12-18 12:03:24

尝试明确地编写专业化:

template <>
friend std::ostream& operator<< <Matrix<char> >(std::ostream& output,
                                       const Matrix<Matrix<char> >& other);

Try writing the specialization explicitly:

template <>
friend std::ostream& operator<< <Matrix<char> >(std::ostream& output,
                                       const Matrix<Matrix<char> >& other);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文