Apply Template Tips to determine the function return value type

xiaoxiao2021-03-06  66

#include using namespace std;

/ / Define the return value type class, determine the return value template struct _return_type {type;};

Template struct _return_type <_tp1, long double> {typedef long double value_type;};

Template struct _return_type {type;};

Template <> struct _return_type {typef int value_type;};

Template <> struct _return_type {typedef float value_type;

Template <> struct _return_type {typef float value_type;};

Template <> struct _return_type {typedef float value_type;

// A function of the maximum value can only be used for the same type of variable. // The reason is that when there are two different types of variables, it cannot be determined which type // is used as the return value. The return value // type can now be easily obtained by template skills. Template inline type <_tp1, _tp2> :: value_typemax (const _tp1 & __a, const _tp2 & __b) {return __a> __ b? __a: __ b;}

Void main () {Max (3, 4.5);

转载请注明原文地址:https://www.9cbs.com/read-88346.html

New Post(0)