简体   繁体   English

模板函数(C ++)中的默认参数

[英]Default parameters in template functions (C++)

I'm trying to use default parameters in a template function. 我正在尝试在模板函数中使用默认参数。 The following is a minimal example of the thing I'm trying to do: 以下是我要做的事情的最小示例:

In sort.h sort.h中

template <typename T0, typename T1>
void sort(Vector<T0> &v0,Vector<T1> &v1=0)
{
    //sort v0, if (v1 != 0) sort it according to v0
}

In main.cpp main.cpp中

#include "sort.h"
Vector<int> v0;
sort(v0);

This does not compile; 这不会编译; the compiler gives the error "no matching function to call to 'sort'". 编译器给出错误“没有匹配的函数来调用'sort'”。

Basically this function should sort the vector v0 (arbitrary datatype). 基本上,此函数应对向量v0(任意数据类型)进行排序。 In addition, a second vector v1 (arbitrary) which is sorted in the same way as vector v0 can be given as parameter. 另外,可以给出以与向量v0相同的方式排序的第二向量v1(任意)作为参数。 Of course I could solve this problem simply by using overloaded functions, but since I'd like to expand the list of additional vectors up to 5, I would need like hundreds of different functions. 当然,我可以简单地通过使用重载函数来解决此问题,但是由于我想将附加向量的列表扩展到5个,因此需要数百种不同的函数。

Update: Thanks for your responses so far. 更新:感谢您到目前为止的答复。 I have modified my problem description to give you a better idea of what I'm trying to do. 我已经修改了问题描述,以使您更好地了解我要做什么。

引用不能指向任何内容。

You cannot default a reference to 0. You'd have to use pointers to do this (if you insist). 您不能将引用默认为0。您必须使用指针来执行此操作(如果您坚持要求的话)。

There's also no way for the compiler to intuit what T1 is without an actual Vector<X> to work from. 如果没有实际的Vector<X> ,编译器也无法了解T1是什么。 A concrete parameter type would be needed for parameter 2. If the second parameter was Vector<T0>* , for example, that would be OK since the type can be divined from parameter 1 (which is not defaulted). 例如,参数2需要一个具体的参数类型。如果第二个参数是Vector<T0>* ,则可以,因为该类型可以与参数1分开(未默认)。 Otherwise, you'll have to specify the template parameters on every call to testfunction . 否则,您必须在每次调用testfunction指定模板参数。

Adding info on what you are trying to achieve might enable better answers to be provided. 添加有关您要达到的目标的信息可能会提供更好的答案。 This seems a little convoluted. 这似乎有点令人费解。

I would just simply overload the function. 我只是简单地重载函数。

template <typename T0, typename T1>
void sort(Vector<T0> &v0,Vector<T1> &v1)
{
    //do something
}
void sort(Vector<T0> &v0)
{
    //do something
}

I would prefer 我会比较喜欢

template <typename T0, typename T1>
void testfunction(Vector<T0> &v0,const Vector<T1> &v1 = Vector<T1>() )
{
    //do something
}

Just because you're giving a default value for the second argument in testfunction, the compiler has no way of deriving T1 if you don't give it something to go on. 仅仅因为您为testfunction中的第二个参数提供了默认值,如果您不给T1继续操作,编译器就无法导出T1。 You need to explicitly state the type. 您需要明确声明类型。 Something like: 就像是:

testfuntion<int, float>(v0);

Also, as others have pointed out, you need to find some way of properly declaring the second argument since a reference can't have "null" value. 另外,正如其他人指出的那样,由于引用不能具有“空”值,因此您需要找到某种方法来正确声明第二个参数。 I personally like this way . 我个人喜欢这种方式

You'll have to change it to use pointers instead: 您必须将其更改为使用指针:

template <typename T0, typename T1>
void testfunction(Vector<T0>* v0, Vector<T1>* v1 = 0)
{
    //do something
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM