gpt4 book ai didi

c++ - 在模板外重载模板类的输出流运算符

转载 作者:太空狗 更新时间:2023-10-29 20:43:02 25 4
gpt4 key购买 nike

我想重载输出流运算符 <<在模板类定义之外。

在模板类中实现它是可以的:

template
<typename T,int _MaxSize=10,template <class C> class Policy=NoCheck,typename Container=std::vector<T>>
class MyContainer : public Policy<T>
{
public:
MyContainer():p(_MaxSize){};
std::ostream& operator<<(MyContainer<T,_MaxSize,Policy,Container>& obj){ };
private:
Container p;
};

但是当我试图在模板类之外进行时:

template
<typename T,int _MaxSize=10,template <class C> class Policy=NoCheck,typename Container=std::vector<T>>
class MyContainer : public Policy<T>
{
public:
MyContainer():p(_MaxSize){};
friend std::ostream& operator<<(std::ostream& out,MyContainer<T,_MaxSize,Policy,Container> obj);
private:
Container p;
};

template
<typename T,int _MaxSize,template <class C> class Policy,typename Container>
std::ostream& operator<<(std::ostream& out,MyContainer<T,_MaxSize,Policy,Container> obj)
{
};

编译器提示:

warning: friend declaration ‘std::ostream& operator<<(std::ostream&, MyContainer<T, _MaxSize, Policy, Container>)’ declares a non-template function [-Wnon-template-friend]
tempstruct.cc:39:97: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)

任何人都可以给出一个简单的例子来说明输出流运算符 <<在模板类之外定义?

在我在这里找到的相关帖子中,每个人都在模板类中进行。

最佳答案

Can anybody give a simple example on how can the output stream operator << defined outside a template class?

不,因为它不简单。我可以举一个复杂的例子:

// Declare the class template, because we need it to declare the operator
template <typename,int,template <class C> class,typename> class MyContainer;

// Declare the operator, because (as the error says) templates must be declared
// in the namespace before you can declare them friends. At this point, we can't
// define the operator, since the class template is incomplete.
template
<typename T,int _MaxSize,template <class C> class Policy,typename Container>
std::ostream& operator<<(std::ostream&,MyContainer<T,_MaxSize,Policy,Container>);

// Define the class template
template
<typename T,int _MaxSize=10,template <class C> class Policy=NoCheck,typename Container=std::vector<T>>
class MyContainer : public Policy<T>
{
public:
MyContainer():p(_MaxSize){};

// Include <> to indicate that this is the template
friend std::ostream& operator<< <>(std::ostream& out,MyContainer<T,_MaxSize,Policy,Container> obj);
private:
Container p;
};

// Finally define the operator
template
<typename T,int _MaxSize,template <class C> class Policy,typename Container>
std::ostream& operator<<(std::ostream& out,MyContainer<T,_MaxSize,Policy,Container> obj)
{
// print some stuff
};

In the related posts that I found here everyone do it inside the template class.

我会那样做;这会简单得多。或者,更好的是,我会根据公共(public)接口(interface)实现输出,假设它提供了对容器内容的足够访问权限。那么你就不需要 friend 声明,所以也不需要前向声明。

关于c++ - 在模板外重载模板类的输出流运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16814037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com