宏与模板??? [英] macro vs. template???

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

问题描述

大家好。一个非常简单的问题。我想知道什么是

在性能方面更好。我想用一个简单的函数来获得两个值的最小值。一种方法可能是使用宏:


#define min(a,b)((a< b)?a:b)


我认为另一种方法可能是使用模板功能:


模板< class T>

T min< T a,T b>

{

if(a< b)

返回a;

else

返回b;

}


任何关于哪种方式更好实现的想法?我想每当我使用模板函数时,我都会帮助我在编译时检查。

另外,由于某些原因我不喜欢使用宏。谢谢。

解决方案

aaragon写道:


大家好。一个非常简单的问题。我想知道什么是

在性能方面更好。我想用一个简单的函数来获得两个值的最小值。一种方法可能是使用宏:


#define min(a,b)((a< b)?a:b)


我认为另一种方法可能是使用模板功能:


模板< class T>

T min< T a,T b>

{

if(a< b)

返回a;

else

返回b;

}


任何关于哪种方式更好实现的想法?我想每当我使用模板函数时,我都会帮助我在编译时检查。

另外,由于某些原因我不喜欢使用宏。谢谢。



你的探查器给你看了什么?


按照你的直觉行事,不要使用宏。


-

Ian Collins。




aaragon写道:


大家好。一个非常简单的问题。我想知道什么是

在性能方面更好。我想用一个简单的函数来获得两个值的最小值。一种方法可能是使用宏:


#define min(a,b)((a< b)?a:b)


我认为另一种方法可能是使用模板功能:


模板< class T>

T min< T a,T b>

{

if(a< b)

返回a;

else

返回b;

}



版本2具有更好的类型安全性。

小功能不会出错,但无论如何,练习在第二节更好。你

可能会考虑引用。


任何关于哪种方式更好实现的想法?我想每当我使用模板函数时,我都会帮助我在编译时检查。

另外,由于某些原因我不喜欢使用宏。谢谢。



没有理由不喜欢使用宏。当

合适时使用它们。宏不适合内联的东西在

C ++中...你应该使用内联函数(不必是模板)。


aaragon写道:


大家好。一个非常简单的问题。我想知道什么是

在性能方面更好。我想用一个简单的函数来获得两个值的最小值。一种方法可能是使用宏:


#define min(a,b)((a< b)?a:b)


我认为另一种方法可能是使用模板功能:


模板< class T>

T min< T a,T b>

{

if(a< b)

返回a;

else

返回b;

}


任何关于哪种方式更好实现的想法?我想每当我使用模板函数时,我都会帮助我在编译时检查。

另外,由于某些原因我不喜欢使用宏。谢谢。



为什么不使用来自< algorithm的std :: min()?

Best


Kai-Uwe Bux


Hi everyone. A very simple question. I would like to know what is
better in terms of performance. I want to use a simple function to
obtain the minimum of two values. One way could be using a macro:

#define min(a,b) ((a<b)?a:b)

I thought that another way could be to use a template function:

template <class T>
T min<T a, T b>
{
if(a < b)
return a;
else
return b;
}

Any ideas on which way is better to implement? I guess that whenever I
use the template function is going to help me check at compile time.
Also, for some reason I don''t like using macros. Thank you.

解决方案

aaragon wrote:

Hi everyone. A very simple question. I would like to know what is
better in terms of performance. I want to use a simple function to
obtain the minimum of two values. One way could be using a macro:

#define min(a,b) ((a<b)?a:b)

I thought that another way could be to use a template function:

template <class T>
T min<T a, T b>
{
if(a < b)
return a;
else
return b;
}

Any ideas on which way is better to implement? I guess that whenever I
use the template function is going to help me check at compile time.
Also, for some reason I don''t like using macros. Thank you.

What did you profiler show you?

Follow your instinct and don''t use macros.

--
Ian Collins.



aaragon wrote:

Hi everyone. A very simple question. I would like to know what is
better in terms of performance. I want to use a simple function to
obtain the minimum of two values. One way could be using a macro:

#define min(a,b) ((a<b)?a:b)

I thought that another way could be to use a template function:

template <class T>
T min<T a, T b>
{
if(a < b)
return a;
else
return b;
}

Version two has better type safety. Not much can go wrong with that
little function but regardless, practice is better in the second. You
might consider references though.

Any ideas on which way is better to implement? I guess that whenever I
use the template function is going to help me check at compile time.
Also, for some reason I don''t like using macros. Thank you.

There is no reason to not like using macros. Use them when
appropriate. Macros just aren''t appropriate for inline stuff in
C++...you should use inline functions (don''t have to be templates).


aaragon wrote:

Hi everyone. A very simple question. I would like to know what is
better in terms of performance. I want to use a simple function to
obtain the minimum of two values. One way could be using a macro:

#define min(a,b) ((a<b)?a:b)

I thought that another way could be to use a template function:

template <class T>
T min<T a, T b>
{
if(a < b)
return a;
else
return b;
}

Any ideas on which way is better to implement? I guess that whenever I
use the template function is going to help me check at compile time.
Also, for some reason I don''t like using macros. Thank you.

Why not use std::min() from <algorithm?
Best

Kai-Uwe Bux


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

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