模板类朋友操作员成员函数

Jad*_*aMD 8 c++ templates friend c++11

我正在尝试在模板化的类中获取一个朋友函数来编译,但错误消息和警告我不明白.我已经证明了这个问题.我得到的错误是:

prog.cpp:8:57:错误:非类,非可变部分特化C运算符+(const B&lhs,const C&rhs);

prog.cpp:15:59:警告:朋友声明'C运算符+(const B&,const C&)'声明一个非模板函数[-Wnon-template-friend]朋友C运算符+(const B&lhs,const C&rhs);

prog.cpp:15:59:注意:(如果这不是你想要的,请确保已经声明了函数模板,并在函数名后添加<>)

#include <iostream>
using namespace std;

template<typename A, typename B>
class C;

template<typename A, typename B>
C<A, B> operator+<A, B>(const B& lhs, const C<A, B>& rhs);

template<typename A, typename B>
struct C
{
    A val_;
    C operator+(const C& other) const;
    friend C<A, B> operator+(const B& lhs, const C<A, B>& rhs);
};

template<typename A, typename B>
C<A, B> C<A, B>::operator+(const C<A, B>& other) const
{
    C<A, B> c;
    c.val_ = this->val_ + other.val_;
    return c;
}

template<typename A, typename B>
 C<A, B> operator+(const B& lhs, const C<A, B>& rhs)
{
    C<A, B> c;
    c.val_ = lhs + rhs.val_;
    return c;
}

int main() 
{
    C<string, char> c0,c1;
    c0.val_ = " C0 ";
    c1.val_ = " C1 ";
    cout << "Stuct:" << (c0 + c1).val_ << '\n';
    cout << "Friend:" << ('~' + c1).val_ << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Jar*_*d42 5

最简单的是在类中内联代码:

template <typename A, typename B>
struct C
{
    A val_;
    C operator+(const C& other) const
    {
        C c;
        c.val_ = this->val_ + other.val_;
        return c;
    }

    friend C operator+ (const B& lhs, const C& rhs)
    {
        C c;
        c.val_ = lhs + rhs.val_;
        return c;
    }
};
Run Code Online (Sandbox Code Playgroud)

演示

代码没有内联在类中,需要很多关注作为声明的前向声明顺序,奇怪的语法<>:

template <typename A, typename B> struct C;

template <typename A, typename B>
C<A, B> operator+ (const B& lhs, const C<A, B>& rhs);

template <typename A, typename B>
struct C
{
    A val_;

    friend C<A, B> operator+<> (const B& lhs, const C<A, B>& rhs);

    C operator+(const C& other) const;
};


template <typename A, typename B>
C<A, B> operator+ (const B& lhs, const C<A, B>& rhs)
{
    C<A, B> c;
    c.val_ = lhs + rhs.val_;
    return c;
}

template <typename A, typename B>
C<A, B> C::operator+(const C<A, B>& other) const
{
    C<A, B> c;
    c.val_ = this->val_ + other.val_;
    return c;
}
Run Code Online (Sandbox Code Playgroud)

演示


Hol*_*olt 3

本声明:

template<typename A, typename B>
C<A, B> operator+<A, B>(const B& lhs, const C<A, B>& rhs);
Run Code Online (Sandbox Code Playgroud)

...因为 和 之间的原因是错误的<A,B>operator+(真的不知道你想在这里做什么。如果您要专门化模板化,您将使用此表单operator+,但您不在这里,您正在重载模板。

该声明应该是:

template<typename A, typename B>
C<A, B> operator+ (const B& lhs, const C<A, B>& rhs);
Run Code Online (Sandbox Code Playgroud)

然后,您应该在声明中明确指定friend您想要一个专门的版本,方法是:

friend C<A,B> operator+<>(const B& lhs, const C<A,B>& rhs);
Run Code Online (Sandbox Code Playgroud)

您需要将其放在 之前operator+,否则编译器会认为这是非模板化函数的特化。

无论如何,如果您没有真正的理由将代码放在C类之外,我会选择@Jarod42 解决方案。


你的整个代码应该是这样的:

// Declaration of struct C with delayed definition
template <typename A, typename B>
struct C;

// Initial declaration of templated operator+
template <typename A, typename B>
C<A, B> operator+ (const B&, const C<A, B>&);

// Definition of C
template <typename A, typename B>
struct C {

    friend C operator+<> (const B&, const C&);

    // This must be AFTER the templated operator+
    C operator+ (const C&) const;
};

template<typename A, typename B>
C<A, B> C<A, B>::operator+(const C<A, B>& other) const {

}

template<typename A, typename B>
C<A, B> operator+(const B& lhs, const C<A, B>& rhs) {

}
Run Code Online (Sandbox Code Playgroud)