模板和宏之间的区别 [英] difference between templates and macros

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

问题描述

您好,

我是c ++和tryig的新手,可以学习c ++概念的基础知识。

当我在阅读模板时,我意识到模板是一个

语法,compilar根据指定的类型扩展。这是一个非常类似于C中的宏观扩展。


任何人都可以解释一个在另一个上的优势吗?


提前致谢 -

San

解决方案

San写道:
< blockquote class =post_quotes>
嗨那里,

我是c ++和tryig的新手,学习c ++概念的基础知识。

在我阅读模板时,我意识到模板是一种

语法,compilar根据指定的类型进行扩展。这是一个非常类似于C中的宏观扩展。


任何人都可以解释一个在另一个上的优势吗?


提前致谢 -

San $​​ b $ b



Hi San,


C ++模板和宏之间存在重大差异。一个

宏只是一个字符串,编译器将其替换为

定义的值。例如,
#define STRING_TO_BE_REPLACED" ValueToReplaceWith"


模板是一种使函数独立于数据类型的方法。这个

无法使用宏来完成。例如。排序功能不会因为相同的

算法可能适用于整数或字母而需要关注它是否正在排序整数或字母。

希望这有帮助。


San写道:


嗨那里,


我是c ++和tryig的新手,可以学习c ++概念的基础知识。

当我在阅读模板时,我意识到模板是一个

compilar根据指定的类型扩展的语法。这与B中的宏扩展很相似。



不是真的,宏是由预处理器扩展的,编译器不会'' t

见到他们。它们是简单的测试替换。


任何人都可以解释一个优点吗?



只有宏的实际优点是能够使用__FILE__和__LINE__调用函数

进行调试。


宏不具备任何范围,简单文字替换。宏用

不能用调试器进入。宏不可能是专业的。

宏不是类型安全的,列表继续。


-

Ian Collins。


San发布:


嗨那里,


我是c ++和tryig的新手,学习c ++概念的基础知识。

当我在阅读模板时,我意识到模板是一个

语法compilar扩展基于指定的类型。这是一个非常类似于C中的宏观扩展。


任何人都可以解释一个在另一个上的优势吗?


提前致谢 -

San $​​ b $ b



如果你想专门化一个宏功能,你必须预先定义它

在其使用的函数之外。考虑一个函数,

返回以null结尾的数组的长度:


使用std :: size_t;


#define DEF(类型)\

size_t LenNullTerm(类型const * p)\

{\

size_t i = 0; \

while(* p ++)++ i; \

返回i; \

}


如果我们想在三种不同类型上使用此功能,我们必须预先确定
如下定义它们如下:


DEF(char)

DEF(双倍)

DEF(int)


int main()

{

int array1 [5] = {3,4};


char array2 [5] = {''a'',''b'',''c''};


double array3 [5] = {5.2,3.4,6.2};


LenNullTerm(array1);

LenNullTerm(array2);

LenNullTerm(array3);

}


使用模板,无需使用DEF预定义函数。<相反,我们只需要:


#include< cstddef>


模板< class T>

size_t LenNullTerm(T const * p)

{

size_t i = 0;

while(* p ++)+ + i;

返回i;

}


int main()

{

int array1 [5] = {3,4};


char array2 [5] = {'''',''b'',' 'c''};


double array3 [5] = {5.2,3.4,6.2};


LenNullTerm(array1);

LenNullTerm(array2);

LenNullTerm(array3);

}


-


Frederick Gotham


hi there,

I am new to c++ and tryig to learn the basics of the c++ concepts.
While I was reading the templates, I realize that the templates are a
syntax that the compilar expands pased upon the type specified. This is
much similar like a macro expansion in C.

can anyone please explain advantages of one over the other ?

Thanks in advance -
San

解决方案

San wrote:

hi there,

I am new to c++ and tryig to learn the basics of the c++ concepts.
While I was reading the templates, I realize that the templates are a
syntax that the compilar expands pased upon the type specified. This is
much similar like a macro expansion in C.

can anyone please explain advantages of one over the other ?

Thanks in advance -
San

Hi San,

In C++ there is a major difference between a template and a macro. A
macro is merely a string that the compiler replaces with the value that
was defined. E.g.
#define STRING_TO_BE_REPLACED "ValueToReplaceWith"

A template is a way to make functions independent of data-types. This
cannot be accomplished using macros. E.g. a sorting function doesn''t
have to care whether it''s sorting integers or letters since the same
algorithm might apply anyway.

Hope this helped.


San wrote:

hi there,

I am new to c++ and tryig to learn the basics of the c++ concepts.
While I was reading the templates, I realize that the templates are a
syntax that the compilar expands pased upon the type specified. This is
much similar like a macro expansion in C.

Not realy, macros are expanded by the preprocessor, the compiler doesn''t
see them. They are simple test substitution.

can anyone please explain advantages of one over the other ?

The only practical advantage of macros is the ability to call a function
with __FILE__ and __LINE__ for debugging.

Macros don''t have any scope, being simple text substitution. Macros
can''t be stepped into with a debugger. Macros can''t be specialised.
Macros aren''t type safe, the list goes on.

--
Ian Collins.


San posted:

hi there,

I am new to c++ and tryig to learn the basics of the c++ concepts.
While I was reading the templates, I realize that the templates are a
syntax that the compilar expands pased upon the type specified. This is
much similar like a macro expansion in C.

can anyone please explain advantages of one over the other ?

Thanks in advance -
San


If you want to specialise a macro function, you have to pre-define it
outside of the function in which it''s used. Consider a function which
returns the length of a null-terminated array:

#include <cstddef>
using std::size_t;

#define DEF(Type)\
size_t LenNullTerm(Type const *p)\
{\
size_t i = 0;\
while(*p++) ++i;\
return i;\
}

If we want to use this function on three different types, we have to pre-
define them as follows:

DEF(char)
DEF(double)
DEF(int)

int main()
{
int array1[5] = {3,4};

char array2[5] = {''a'',''b'',''c''};

double array3[5] = {5.2,3.4,6.2};

LenNullTerm(array1);
LenNullTerm(array2);
LenNullTerm(array3);
}

With templates, there''s no need to pre-define the function with "DEF".
Instead, we just have:

#include <cstddef>

template<class T>
size_t LenNullTerm(T const *p)
{
size_t i = 0;
while(*p++) ++i;
return i;
}

int main()
{
int array1[5] = {3,4};

char array2[5] = {''a'',''b'',''c''};

double array3[5] = {5.2,3.4,6.2};

LenNullTerm(array1);
LenNullTerm(array2);
LenNullTerm(array3);
}

--

Frederick Gotham


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

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