功能模板 [英] function template

查看:66
本文介绍了功能模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用一个简单的函数模板来查找两个值的最小值。

但是因为我希望这两个值不同( type)我不知道

它会返回什么样的值(类型)。我试着写这样的东西,比如

这个:


模板<类Type1,类Type2,类Type3>

Type3 findMin (Type1 x,Type2 y){


return(x< y)? x:y;

}


但是它说它不能推断''Type3'的模板参数'

任何人都可以帮忙? Thanx

Hi all,
I was working with a simple function template to find the min of two values.
But since I would like the two values to be different (type) I dont know
what kind of value (type) it will return. I tried to write something like
this:

template <class Type1, class Type2, class Type3>
Type3 findMin(Type1 x, Type2 y){

return (x < y) ? x : y;
}

but it says it cannot deduce template argument for ''Type3''
Can anyone help? Thanx

推荐答案

" nikola" < SE ****** @ here.lol>在留言中写道

新闻:Zk *********************** @ news3.tin.it ...
"nikola" <se******@here.lol> wrote in message
news:Zk***********************@news3.tin.it...
大家好,
我正在使用一个简单的函数模板来查找两个
值的最小值。但是因为我希望这两个值不同(类型)我不知道
它会返回什么样的值(类型)。我试着写这样的东西:

模板<类Type1,类Type2,类Type3>
Type3 findMin(Type1 x,Type2 y){

返回(x }

但是它说不能推断''Type3'的模板参数。
任何人都可以帮忙吗? Thanx
Hi all,
I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know
what kind of value (type) it will return. I tried to write something like
this:

template <class Type1, class Type2, class Type3>
Type3 findMin(Type1 x, Type2 y){

return (x < y) ? x : y;
}

but it says it cannot deduce template argument for ''Type3''
Can anyone help? Thanx




你好


试着把它称为:


findMin< int ,int,int>(1,2);


就像你指定了所有类型一样。


-


Elias



Hello

Try calling it as:

findMin<int, int, int>(1, 2);

Like that you have specified all the types.

--

Elias


nikola写道:
nikola wrote:
大家好,
我当时正在与一个简单的函数模板,用于查找最小值的两个值。但是因为我希望这两个值不同(类型)我不知道它会返回什么样的值(类型)。我试着写这样的东西:

模板<类Type1,类Type2,类Type3>
Type3 findMin(Type1 x,Type2 y){

返回(x }

但它说它不能推断''Type3''的模板参数
Hi all,
I was working with a simple function template to find the min of two
values. But since I would like the two values to be different (type) I
dont know what kind of value (type) it will return. I tried to write
something like this:

template <class Type1, class Type2, class Type3>
Type3 findMin(Type1 x, Type2 y){

return (x < y) ? x : y;
}

but it says it cannot deduce template argument for ''Type3''




嗯,首先总之,函数的返回类型是在编译

时固定的。编译器如何知道返回哪一个?

Seond,?:运算符的第二和第三个参数也必须是

相同的类型,所以无论如何,你的一个参数转换为另一个'

类型。类似于运营商<。所以你可以写:


模板<类类型>

类型findMin(类型x,类型y){

返回(x
}



Well, first of all, the return type of a function is fixed at compile
time. And how would the compiler know which one to return?
Seond, the 2nd and 3rd argument of the ?: operator need to be of the
same type, too, so one of your arguments is converted to the other''s
type anyway. Similar for operator<. So you can just write:

template <class Type>
Type findMin(Type x, Type y){
return (x < y) ? x : y;
}


nikola写道:
大家好,
我正在使用一个简单的函数模板来查找两个值的最小值。
但是因为我希望这两个值不同(类型)我不知道
它会是什么样的值(类型)返回。我试着写这样的东西:

模板<类Type1,类Type2,类Type3>
Type3 findMin(Type1 x,Type2 y){

返回(x }

但是它说不能推断''Type3'的模板参数。
任何人都可以帮忙吗? Thanx
Hi all,
I was working with a simple function template to find the min of two values.
But since I would like the two values to be different (type) I dont know
what kind of value (type) it will return. I tried to write something like
this:

template <class Type1, class Type2, class Type3>
Type3 findMin(Type1 x, Type2 y){

return (x < y) ? x : y;
}

but it says it cannot deduce template argument for ''Type3''
Can anyone help? Thanx




这是一个经典的模板问题。


你真正想要的是:


模板< typename T1,typename T2>

typename提升< T1,T2> :: type min(const T1& x,const T2& y)

{

返回x<你? x:y;

}


其中,Promote是一个模板,可以执行正确的促销活动。对于

T1和T2。


如果是typeof()函数是标准的,你可以简单地这样做:

模板< typename T1,typename T2>

struct推广

{

typedef typeof(T1()+ T2())类型;

};


....但是唉,事情并非如此简单的标准世界。


我们需要自己解决这个问题,所以我们必须使用部分模板

专业化。


从基本模板开始:

模板< typename T1,typename T2>

struct推广

{

};


//相同的类型是相同的!很酷


模板< typename T1>

struct推广< T1,T1>

{

typedef T1类型;

};


//指定所有类型的促销活动...


模板<> struct Promote< unsigned char,char> {typedef int type; };

模板<> struct Promote< signed char,char> {typedef int type; };

模板<> struct Promote< short,char> {typedef int type; };

模板<> struct Promote< unsigned short,char> {typedef int type; };

模板<> struct Promote< int,char> {typedef int type; };

模板<> struct Promote< unsigned int,char> {typedef unsigned int

type; };

模板<> struct Promote< long,char> {typedef long type; };

模板<> struct Promote< unsigned long,char> {typedef unsigned long

type; };

模板<> struct Promote< long long,char> {typedef long long type; };

模板<> struct Promote< unsigned long long,char> {typedef unsigned

long long type; };

模板<> struct Promote< float,char> {typedef float type; };

模板<> struct Promote< double,char> {typedef double type; };

模板<> struct Promote< long double,char> {typedef long double type; };

模板<> struct Promote< char,unsigned char> {typedef int type; };

模板<> struct Promote< signed char,unsigned char> {typedef int type; };

模板<> struct Promote< short,unsigned char> {typedef int type; };

模板<> struct Promote< unsigned short,unsigned char> {typedef int

type; };

模板<> struct Promote< int,unsigned char> {typedef int type; };

模板<> struct Promote< unsigned int,unsigned char> {typedef

unsigned int type; };

模板<> struct Promote< long,unsigned char> {typedef long type; };

模板<> struct Promote< unsigned long,unsigned char> {typedef

unsigned long type; };

模板<> struct Promote< long long,unsigned char> {typedef long long

type; };

模板<> struct Promote< unsigned long long,unsigned char> {typedef

unsigned long long type; };

模板<> struct Promote< float,unsigned char> {typedef float type; };

模板<> struct Promote< double,unsigned char> {typedef double type; };

模板<> struct Promote< long double,unsigned char> {typedef long

double type; };

模板<> struct Promote< char,signed char> {typedef int type; };

模板<> struct Promote< unsigned char,signed char> {typedef int type; };

模板<> struct Promote< short,signed char> {typedef int type; };

模板<> struct Promote< unsigned short,signed char> {typedef int

type; };

模板<> struct Promote< int,signed char> {typedef int type; };

模板<> struct Promote< unsigned int,signed char> {typedef unsigned

int type; };

模板<> struct Promote< long,signed char> {typedef long type; };

模板<> struct Promote< unsigned long,signed char> {typedef unsigned

long type; };

模板<> struct Promote< long long,signed char> {typedef long long

type; };

模板<> struct Promote< unsigned long long,signed char> {typedef

unsigned long long type; };

模板<> struct Promote< float,signed char> {typedef float type; };

模板<> struct Promote< double,signed char> {typedef double type; };

模板<> struct Promote< long double,signed char> {typedef long

double type; };

模板<> struct Promote< char,short> {typedef int type; };

模板<> struct Promote< unsigned char,short> {typedef int type; };

模板<> struct Promote< signed char,short> {typedef int type; };

模板<> struct Promote< unsigned short,short> {typedef int type; };

模板<> struct Promote< int,short> {typedef int type; };

模板<> struct Promote< unsigned int,short> {typedef unsigned int

type; };

模板<> struct Promote< long,short> {typedef long type; };

模板<> struct Promote< unsigned long,short> {typedef unsigned long

type; };

模板<> struct Promote< long long,short> {typedef long long type; };

模板<> struct Promote< unsigned long long,short> {typedef unsigned

long long type; };

模板<> struct Promote< float,short> {typedef float type; };

模板<> struct Promote< double,short> {typedef double type; };

模板<> struct Promote< long double,short> {typedef long double type; };

模板<> struct Promote< char,unsigned short> {typedef int type; };

模板<> struct Promote< unsigned char,unsigned short> {typedef int

type; };

模板<> struct Promote< signed char,unsigned short> {typedef int

type; };

模板<> struct Promote< short,unsigned short> {typedef int type; };

模板<> struct Promote< int,unsigned short> {typedef int type; };

模板<> struct Promote< unsigned int,unsigned short> {typedef

unsigned int type; };

模板<> struct Promote< long,unsigned short> {typedef long type; };

模板<> struct Promote< unsigned long,unsigned short> {typedef

unsigned long type; };

模板<> struct Promote< long long,unsigned short> {typedef long long

type; };

模板<> struct Promote< unsigned long long,unsigned short> {typedef

unsigned long long type; };

模板<> struct Promote< float,unsigned short> {typedef float type; };

模板<> struct Promote< double,unsigned short> {typedef double type; };

模板<> struct Promote< long double,unsigned short> {typedef long

double type; };

模板<> struct Promote< char,int> {typedef int type; };

模板<> struct Promote< unsigned char,int> {typedef int type; };

模板<> struct Promote< signed char,int> {typedef int type; };

模板<> struct Promote< short,int> {typedef int type; };

模板<> struct Promote< unsigned short,int> {typedef int type; };

模板<> struct Promote< unsigned int,int> {typedef unsigned int type; };

模板<> struct Promote< long,int> {typedef long type; };

模板<> struct Promote< unsigned long,int> {typedef unsigned long

type; };

模板<> struct Promote< long long,int> {typedef long long type; };

模板<> struct Promote< unsigned long long,int> {typedef unsigned

long long type; };

模板<> struct Promote< float,int> {typedef float type; };

模板<> struct Promote< double,int> {typedef double type; };

模板<> struct Promote< long double,int> {typedef long double type; };

模板<> struct Promote< char,unsigned int> {typedef unsigned int

type; };

模板<> struct Promote< unsigned char,unsigned int> {typedef

unsigned int type; };

模板<> struct Promote< signed char,unsigned int> {typedef unsigned

int type; };

模板<> struct Promote< short,unsigned int> {typedef unsigned int

type; };

模板<> struct Promote< unsigned short,unsigned int> {typedef

unsigned int type; };

模板<> struct Promote< int,unsigned int> {typedef unsigned int type; };

模板<> struct Promote< long,unsigned int> {typedef unsigned long

type; };

模板<> struct Promote< unsigned long,unsigned int> {typedef

unsigned long type; };

模板<> struct Promote< long long,unsigned int> {typedef long long

type; };

模板<> struct Promote< unsigned long long,unsigned int> {typedef

unsigned long long type; };

模板<> struct Promote< float,unsigned int> {typedef float type; };

模板<> struct Promote< double,unsigned int> {typedef double type; };

模板<> struct Promote< long double,unsigned int> {typedef long

double type; };

模板<> struct Promote< char,long> {typedef long type; };

模板<> struct Promote< unsigned char,long> {typedef long type; };

模板<> struct Promote< signed char,long> {typedef long type; };

模板<> struct Promote< short,long> {typedef long type; };

模板<> struct Promote< unsigned short,long> {typedef long type; };

模板<> struct Promote< int,long> {typedef long type; };

模板<> struct Promote< unsigned int,long> {typedef unsigned long

type; };

模板<> struct Promote< unsigned long,long> {typedef unsigned long

type; };

模板<> struct Promote< long long,long> {typedef long long type; };

模板<> struct Promote< unsigned long long,long> {typedef unsigned

long long type; };

模板<> struct Promote< float,long> {typedef float type; };

模板<> struct Promote< double,long> {typedef double type; };

模板<> struct Promote< long double,long> {typedef long double type; };

模板<> struct Promote< char,unsigned long> {typedef unsigned long

type; };

模板<> struct Promote< unsigned char,unsigned long> {typedef

unsigned long type; };

模板<> struct Promote< signed char,unsigned long> {typedef unsigned

long type; };

模板<> struct Promote< short,unsigned long> {typedef unsigned long

type; };

模板<> struct Promote< unsigned short,unsigned long> {typedef

unsigned long type; };

模板<> struct Promote< int,unsigned long> {typedef unsigned long

type; };

模板<> struct Promote< unsigned int,unsigned long> {typedef

unsigned long type; };

模板<> struct Promote< long,unsigned long> {typedef unsigned long

type; };

模板<> struct Promote< long long,unsigned long> {typedef long long

type; };

模板<> struct Promote< unsigned long long,unsigned long> {typedef

unsigned long long type; };

模板<> struct Promote< float,unsigned long> {typedef float type; };

模板<> struct Promote< double,unsigned long> {typedef double type; };

模板<> struct Promote< long double,unsigned long> {typedef long

double type; };

模板<> struct Promote< char,long long> {typedef long long type; };

模板<> struct Promote< unsigned char,long long> {typedef long long

type; };

模板<> struct Promote< signed char,long long> {typedef long long

type; };

模板<> struct Promote< short,long long> {typedef long long type; };

模板<> struct Promote< unsigned short,long long> {typedef long long

type; };

模板<> struct Promote< int,long long> {typedef long long type; };

模板<> struct Promote< unsigned int,long long> {typedef long long

type; };

模板<> struct Promote< long,long long> {typedef long long type; };

模板<> struct Promote< unsigned long,long long> {typedef long long

type; };

模板<> struct Promote< unsigned long long,long long> {typedef

unsigned long long type; };

模板<> struct Promote< float,long long> {typedef float type; };

模板<> struct Promote< double,long long> {typedef double type; };

模板<> struct Promote< long double,long long> {typedef long double

type; };

模板<> struct Promote< char,unsigned long long> {typedef unsigned

long long type; };

模板<> struct Promote< unsigned char,unsigned long long> {typedef

unsigned long long type; };

模板<> struct Promote< signed char,unsigned long long> {typedef

unsigned long long type; };

模板<> struct Promote< short,unsigned long long> {typedef unsigned

long long type; };

模板<> struct Promote< unsigned short,unsigned long long> {typedef

unsigned long long type; };

模板<> struct Promote< int,unsigned long long> {typedef unsigned

long long type; };

模板<> struct Promote< unsigned int,unsigned long long> {typedef

unsigned long long type; };

模板<> struct Promote< long,unsigned long long> {typedef unsigned

long long type; };

模板<> struct Promote< unsigned long,unsigned long long> {typedef

unsigned long long type; };

模板<> struct Promote< long long,unsigned long long> {typedef

unsigned long long type; };

模板<> struct Promote< float,unsigned long long> {typedef float

type; };

模板<> struct Promote< double,unsigned long long> {typedef double

type; };

模板<> struct Promote< long double,unsigned long long> {typedef

long double type; };

模板<> struct Promote< char,float> {typedef float type; };

模板<> struct Promote< unsigned char,float> {typedef float type; };

模板<> struct Promote< signed char,float> {typedef float type; };

模板<> struct Promote< short,float> {typedef float type; };

模板<> struct Promote< unsigned short,float> {typedef float type; };

模板<> struct Promote< int,float> {typedef float type; };

模板<> struct Promote< unsigned int,float> {typedef float type; };

模板<> struct Promote< long,float> {typedef float type; };

模板<> struct Promote< unsigned long,float> {typedef float type; };

模板<> struct Promote< long long,float> {typedef float type; };

模板<> struct Promote< unsigned long long,float> {typedef float

type; };

模板<> struct Promote< double,float> {typedef double type; };

模板<> struct Promote< long double,float> {typedef long double type; };

模板<> struct Promote< char,double> {typedef double type; };

模板<> struct Promote< unsigned char,double> {typedef double type; };

模板<> struct Promote< signed char,double> {typedef double type; };

模板<> struct Promote< short,double> {typedef double type; };

模板<> struct Promote< unsigned short,double> {typedef double type; };

模板<> struct Promote< int,double> {typedef double type; };

模板<> struct Promote< unsigned int,double> {typedef double type; };

模板<> struct Promote< long,double> {typedef double type; };

模板<> struct Promote< unsigned long,double> {typedef double type; };

模板<> struct Promote< long long,double> {typedef double type; };

模板<> struct Promote< unsigned long long,double> {typedef double

type; };

模板<> struct Promote< float,double> {typedef double type; };

模板<> struct Promote< long double,double> {typedef long double

type; };

模板<> struct Promote< char,long double> {typedef long double type; };

模板<> struct Promote< unsigned char,long double> {typedef long

double type; };

模板<> struct Promote< signed char,long double> {typedef long

double type; };

模板<> struct Promote< short,long double> {typedef long double type; };

模板<> struct Promote< unsigned short,long double> {typedef long

double type; };

模板<> struct Promote< int,long double> {typedef long double type; };

模板<> struct Promote< unsigned int,long double> {typedef long

double type; };

模板<> struct Promote< long,long double> {typedef long double type; };

模板<> struct Promote< unsigned long,long double> {typedef long

double type; };

模板<> struct Promote< long long,long double> {typedef long double

type; };

模板<> struct Promote< unsigned long long,long double> {typedef

long double type; };

模板<> struct Promote< float,long double> {typedef long double type; };

模板<> struct Promote< double,long double> {typedef long double

type; };


//

//现在是最小模板......


模板< typename T1,typename T2>

typename提升< T1,T2> ::类型min(const T1& x,const T2& y)

{

返回x<你? x:y;

}


这有点长啰嗦。然而,有些人可能会认为这种类型

促销是一件坏事,开发人员真的需要确保
确保min(或max)的参数是相同的类型,其中

案例模板变得容易编写。



This is a classic template issue.

What you really want is :

template <typename T1, typename T2>
typename Promote<T1,T2>::type min( const T1 & x, const T2 & y )
{
return x < y ? x : y;
}

Where Promote is a template that will perform the right "promotion" for
T1 and T2.

If the "typeof()" function was standard, you could simply do this:
template <typename T1, typename T2>
struct Promote
{
typedef typeof( T1() + T2() ) type;
};

.... but alas, things are not so simple in the standard world.

We need to figure it out ourselves so we have to use partial template
specialization.

Start with the basic template:
template <typename T1, typename T2>
struct Promote
{
};

// the same types are the same ! cool

template <typename T1>
struct Promote<T1,T1>
{
typedef T1 type;
};

// specify all the type promotions ...

template <> struct Promote<unsigned char,char> { typedef int type; };
template <> struct Promote<signed char,char> { typedef int type; };
template <> struct Promote<short,char> { typedef int type; };
template <> struct Promote<unsigned short,char> { typedef int type; };
template <> struct Promote<int,char> { typedef int type; };
template <> struct Promote<unsigned int,char> { typedef unsigned int
type; };
template <> struct Promote<long,char> { typedef long type; };
template <> struct Promote<unsigned long,char> { typedef unsigned long
type; };
template <> struct Promote<long long,char> { typedef long long type; };
template <> struct Promote<unsigned long long,char> { typedef unsigned
long long type; };
template <> struct Promote<float,char> { typedef float type; };
template <> struct Promote<double,char> { typedef double type; };
template <> struct Promote<long double,char> { typedef long double type; };
template <> struct Promote<char,unsigned char> { typedef int type; };
template <> struct Promote<signed char,unsigned char> { typedef int type; };
template <> struct Promote<short,unsigned char> { typedef int type; };
template <> struct Promote<unsigned short,unsigned char> { typedef int
type; };
template <> struct Promote<int,unsigned char> { typedef int type; };
template <> struct Promote<unsigned int,unsigned char> { typedef
unsigned int type; };
template <> struct Promote<long,unsigned char> { typedef long type; };
template <> struct Promote<unsigned long,unsigned char> { typedef
unsigned long type; };
template <> struct Promote<long long,unsigned char> { typedef long long
type; };
template <> struct Promote<unsigned long long,unsigned char> { typedef
unsigned long long type; };
template <> struct Promote<float,unsigned char> { typedef float type; };
template <> struct Promote<double,unsigned char> { typedef double type; };
template <> struct Promote<long double,unsigned char> { typedef long
double type; };
template <> struct Promote<char,signed char> { typedef int type; };
template <> struct Promote<unsigned char,signed char> { typedef int type; };
template <> struct Promote<short,signed char> { typedef int type; };
template <> struct Promote<unsigned short,signed char> { typedef int
type; };
template <> struct Promote<int,signed char> { typedef int type; };
template <> struct Promote<unsigned int,signed char> { typedef unsigned
int type; };
template <> struct Promote<long,signed char> { typedef long type; };
template <> struct Promote<unsigned long,signed char> { typedef unsigned
long type; };
template <> struct Promote<long long,signed char> { typedef long long
type; };
template <> struct Promote<unsigned long long,signed char> { typedef
unsigned long long type; };
template <> struct Promote<float,signed char> { typedef float type; };
template <> struct Promote<double,signed char> { typedef double type; };
template <> struct Promote<long double,signed char> { typedef long
double type; };
template <> struct Promote<char,short> { typedef int type; };
template <> struct Promote<unsigned char,short> { typedef int type; };
template <> struct Promote<signed char,short> { typedef int type; };
template <> struct Promote<unsigned short,short> { typedef int type; };
template <> struct Promote<int,short> { typedef int type; };
template <> struct Promote<unsigned int,short> { typedef unsigned int
type; };
template <> struct Promote<long,short> { typedef long type; };
template <> struct Promote<unsigned long,short> { typedef unsigned long
type; };
template <> struct Promote<long long,short> { typedef long long type; };
template <> struct Promote<unsigned long long,short> { typedef unsigned
long long type; };
template <> struct Promote<float,short> { typedef float type; };
template <> struct Promote<double,short> { typedef double type; };
template <> struct Promote<long double,short> { typedef long double type; };
template <> struct Promote<char,unsigned short> { typedef int type; };
template <> struct Promote<unsigned char,unsigned short> { typedef int
type; };
template <> struct Promote<signed char,unsigned short> { typedef int
type; };
template <> struct Promote<short,unsigned short> { typedef int type; };
template <> struct Promote<int,unsigned short> { typedef int type; };
template <> struct Promote<unsigned int,unsigned short> { typedef
unsigned int type; };
template <> struct Promote<long,unsigned short> { typedef long type; };
template <> struct Promote<unsigned long,unsigned short> { typedef
unsigned long type; };
template <> struct Promote<long long,unsigned short> { typedef long long
type; };
template <> struct Promote<unsigned long long,unsigned short> { typedef
unsigned long long type; };
template <> struct Promote<float,unsigned short> { typedef float type; };
template <> struct Promote<double,unsigned short> { typedef double type; };
template <> struct Promote<long double,unsigned short> { typedef long
double type; };
template <> struct Promote<char,int> { typedef int type; };
template <> struct Promote<unsigned char,int> { typedef int type; };
template <> struct Promote<signed char,int> { typedef int type; };
template <> struct Promote<short,int> { typedef int type; };
template <> struct Promote<unsigned short,int> { typedef int type; };
template <> struct Promote<unsigned int,int> { typedef unsigned int type; };
template <> struct Promote<long,int> { typedef long type; };
template <> struct Promote<unsigned long,int> { typedef unsigned long
type; };
template <> struct Promote<long long,int> { typedef long long type; };
template <> struct Promote<unsigned long long,int> { typedef unsigned
long long type; };
template <> struct Promote<float,int> { typedef float type; };
template <> struct Promote<double,int> { typedef double type; };
template <> struct Promote<long double,int> { typedef long double type; };
template <> struct Promote<char,unsigned int> { typedef unsigned int
type; };
template <> struct Promote<unsigned char,unsigned int> { typedef
unsigned int type; };
template <> struct Promote<signed char,unsigned int> { typedef unsigned
int type; };
template <> struct Promote<short,unsigned int> { typedef unsigned int
type; };
template <> struct Promote<unsigned short,unsigned int> { typedef
unsigned int type; };
template <> struct Promote<int,unsigned int> { typedef unsigned int type; };
template <> struct Promote<long,unsigned int> { typedef unsigned long
type; };
template <> struct Promote<unsigned long,unsigned int> { typedef
unsigned long type; };
template <> struct Promote<long long,unsigned int> { typedef long long
type; };
template <> struct Promote<unsigned long long,unsigned int> { typedef
unsigned long long type; };
template <> struct Promote<float,unsigned int> { typedef float type; };
template <> struct Promote<double,unsigned int> { typedef double type; };
template <> struct Promote<long double,unsigned int> { typedef long
double type; };
template <> struct Promote<char,long> { typedef long type; };
template <> struct Promote<unsigned char,long> { typedef long type; };
template <> struct Promote<signed char,long> { typedef long type; };
template <> struct Promote<short,long> { typedef long type; };
template <> struct Promote<unsigned short,long> { typedef long type; };
template <> struct Promote<int,long> { typedef long type; };
template <> struct Promote<unsigned int,long> { typedef unsigned long
type; };
template <> struct Promote<unsigned long,long> { typedef unsigned long
type; };
template <> struct Promote<long long,long> { typedef long long type; };
template <> struct Promote<unsigned long long,long> { typedef unsigned
long long type; };
template <> struct Promote<float,long> { typedef float type; };
template <> struct Promote<double,long> { typedef double type; };
template <> struct Promote<long double,long> { typedef long double type; };
template <> struct Promote<char,unsigned long> { typedef unsigned long
type; };
template <> struct Promote<unsigned char,unsigned long> { typedef
unsigned long type; };
template <> struct Promote<signed char,unsigned long> { typedef unsigned
long type; };
template <> struct Promote<short,unsigned long> { typedef unsigned long
type; };
template <> struct Promote<unsigned short,unsigned long> { typedef
unsigned long type; };
template <> struct Promote<int,unsigned long> { typedef unsigned long
type; };
template <> struct Promote<unsigned int,unsigned long> { typedef
unsigned long type; };
template <> struct Promote<long,unsigned long> { typedef unsigned long
type; };
template <> struct Promote<long long,unsigned long> { typedef long long
type; };
template <> struct Promote<unsigned long long,unsigned long> { typedef
unsigned long long type; };
template <> struct Promote<float,unsigned long> { typedef float type; };
template <> struct Promote<double,unsigned long> { typedef double type; };
template <> struct Promote<long double,unsigned long> { typedef long
double type; };
template <> struct Promote<char,long long> { typedef long long type; };
template <> struct Promote<unsigned char,long long> { typedef long long
type; };
template <> struct Promote<signed char,long long> { typedef long long
type; };
template <> struct Promote<short,long long> { typedef long long type; };
template <> struct Promote<unsigned short,long long> { typedef long long
type; };
template <> struct Promote<int,long long> { typedef long long type; };
template <> struct Promote<unsigned int,long long> { typedef long long
type; };
template <> struct Promote<long,long long> { typedef long long type; };
template <> struct Promote<unsigned long,long long> { typedef long long
type; };
template <> struct Promote<unsigned long long,long long> { typedef
unsigned long long type; };
template <> struct Promote<float,long long> { typedef float type; };
template <> struct Promote<double,long long> { typedef double type; };
template <> struct Promote<long double,long long> { typedef long double
type; };
template <> struct Promote<char,unsigned long long> { typedef unsigned
long long type; };
template <> struct Promote<unsigned char,unsigned long long> { typedef
unsigned long long type; };
template <> struct Promote<signed char,unsigned long long> { typedef
unsigned long long type; };
template <> struct Promote<short,unsigned long long> { typedef unsigned
long long type; };
template <> struct Promote<unsigned short,unsigned long long> { typedef
unsigned long long type; };
template <> struct Promote<int,unsigned long long> { typedef unsigned
long long type; };
template <> struct Promote<unsigned int,unsigned long long> { typedef
unsigned long long type; };
template <> struct Promote<long,unsigned long long> { typedef unsigned
long long type; };
template <> struct Promote<unsigned long,unsigned long long> { typedef
unsigned long long type; };
template <> struct Promote<long long,unsigned long long> { typedef
unsigned long long type; };
template <> struct Promote<float,unsigned long long> { typedef float
type; };
template <> struct Promote<double,unsigned long long> { typedef double
type; };
template <> struct Promote<long double,unsigned long long> { typedef
long double type; };
template <> struct Promote<char,float> { typedef float type; };
template <> struct Promote<unsigned char,float> { typedef float type; };
template <> struct Promote<signed char,float> { typedef float type; };
template <> struct Promote<short,float> { typedef float type; };
template <> struct Promote<unsigned short,float> { typedef float type; };
template <> struct Promote<int,float> { typedef float type; };
template <> struct Promote<unsigned int,float> { typedef float type; };
template <> struct Promote<long,float> { typedef float type; };
template <> struct Promote<unsigned long,float> { typedef float type; };
template <> struct Promote<long long,float> { typedef float type; };
template <> struct Promote<unsigned long long,float> { typedef float
type; };
template <> struct Promote<double,float> { typedef double type; };
template <> struct Promote<long double,float> { typedef long double type; };
template <> struct Promote<char,double> { typedef double type; };
template <> struct Promote<unsigned char,double> { typedef double type; };
template <> struct Promote<signed char,double> { typedef double type; };
template <> struct Promote<short,double> { typedef double type; };
template <> struct Promote<unsigned short,double> { typedef double type; };
template <> struct Promote<int,double> { typedef double type; };
template <> struct Promote<unsigned int,double> { typedef double type; };
template <> struct Promote<long,double> { typedef double type; };
template <> struct Promote<unsigned long,double> { typedef double type; };
template <> struct Promote<long long,double> { typedef double type; };
template <> struct Promote<unsigned long long,double> { typedef double
type; };
template <> struct Promote<float,double> { typedef double type; };
template <> struct Promote<long double,double> { typedef long double
type; };
template <> struct Promote<char,long double> { typedef long double type; };
template <> struct Promote<unsigned char,long double> { typedef long
double type; };
template <> struct Promote<signed char,long double> { typedef long
double type; };
template <> struct Promote<short,long double> { typedef long double type; };
template <> struct Promote<unsigned short,long double> { typedef long
double type; };
template <> struct Promote<int,long double> { typedef long double type; };
template <> struct Promote<unsigned int,long double> { typedef long
double type; };
template <> struct Promote<long,long double> { typedef long double type; };
template <> struct Promote<unsigned long,long double> { typedef long
double type; };
template <> struct Promote<long long,long double> { typedef long double
type; };
template <> struct Promote<unsigned long long,long double> { typedef
long double type; };
template <> struct Promote<float,long double> { typedef long double type; };
template <> struct Promote<double,long double> { typedef long double
type; };

//
// and now the min template ...

template <typename T1, typename T2>
typename Promote<T1,T2>::type min( const T1 & x, const T2 & y )
{
return x < y ? x : y;
}

That''s a bit long winded. However, some people might arge that type
promotion is a bad thing and that the developer really needs to make
sure that the parameters to min (or max) are the same type, in which
case the template becomes easy to write.


这篇关于功能模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆