使用模板类作为参数

时间:2021-07-22 21:39:30

Is there any way of creating a function that accepts any version of a given template class?

有没有办法创建一个接受给定模板类的任何版本的函数?

e.g. this works:

例如这工作:

ostream& operator << (ostream &out,const Vector<int>& vec);

but this doesn't:

但这不是:

ostream& operator << (ostream &out,const Vector& vec);

Is it possible to get the second line to work somehow for any version of vector? e.g. vector<int> and vector<double> without having to write 2 separate functions?

对于任何版本的向量,是否有可能以某种方式使第二行工作?例如vector 和vector 而不必编写2个单独的函数?

Added to question:

添加到问题:

I've made op<< a template function like you've suggested. In order to make it a friend function of the vector class I tried adding the following to the Vector class definition, but it didn't work:

我已经将op < <模板函数设为你所建议的。为了使它成为vector类的友元函数,我尝试将以下内容添加到vector类定义中,但它不起作用:< p>

friend ostream& operator << (ostream &out, const Vector<T>& vec); 

any ideas what can be done to fix it?

有什么想法可以解决它吗?

3 个解决方案

#1


As already pointed out something like this should work:

正如已经指出的那样应该有效:

template <typename T>
ostream& operator << (ostream &out,const Vector<T>& vec) {
    // body here
}

As for the friend requirement, that is most easily handled like this:

至于朋友的要求,最容易处理如下:

template <typename T>
ostream& operator << (ostream &out,const Vector<T>& vec) {
    vec.print_on( out );
    return out;
}

However, normally I would think of any output operator like this that requires access to the internals of the class to be showing you a mistake in your Vector class. It really only ought to need to use the public interface to do the display.

但是,通常我会想到这样的任何输出操作符需要访问类的内部,以便在Vector类中显示错误。它实际上只需要使用公共接口来进行显示。

The other thing is that you might also want to template the output stream itself so that you can preserve its type:

另一件事是您可能还想模拟输出流本身,以便您可以保留其类型:

template <typename O, typename C, typename T>
std::basic_ostream<O, C>& operator << (std::basic_ostream<O, C> &out,const Vector<T>& vec) {
    vec.print_on( out );
    return out;
}

The Vector's print_on can still use ostream.

Vector的print_on仍然可以使用ostream。

#2


Sure, make it a template function:

当然,让它成为模板功能:

template <typename T>
ostream& operator << (ostream &out,const Vector<T>& vec);

#3


template <class T>
ostream& operator << (ostream &out,const Vector<T>& vec);

#1


As already pointed out something like this should work:

正如已经指出的那样应该有效:

template <typename T>
ostream& operator << (ostream &out,const Vector<T>& vec) {
    // body here
}

As for the friend requirement, that is most easily handled like this:

至于朋友的要求,最容易处理如下:

template <typename T>
ostream& operator << (ostream &out,const Vector<T>& vec) {
    vec.print_on( out );
    return out;
}

However, normally I would think of any output operator like this that requires access to the internals of the class to be showing you a mistake in your Vector class. It really only ought to need to use the public interface to do the display.

但是,通常我会想到这样的任何输出操作符需要访问类的内部,以便在Vector类中显示错误。它实际上只需要使用公共接口来进行显示。

The other thing is that you might also want to template the output stream itself so that you can preserve its type:

另一件事是您可能还想模拟输出流本身,以便您可以保留其类型:

template <typename O, typename C, typename T>
std::basic_ostream<O, C>& operator << (std::basic_ostream<O, C> &out,const Vector<T>& vec) {
    vec.print_on( out );
    return out;
}

The Vector's print_on can still use ostream.

Vector的print_on仍然可以使用ostream。

#2


Sure, make it a template function:

当然,让它成为模板功能:

template <typename T>
ostream& operator << (ostream &out,const Vector<T>& vec);

#3


template <class T>
ostream& operator << (ostream &out,const Vector<T>& vec);