确认对 typedef 和 #define 的理解 [英] confirm understanding of typedef and #define

查看:27
本文介绍了确认对 typedef 和 #define 的理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多教程和问题可以解决这个问题.但我想在一个具体案例中确认我的理解.下面的两个不应该对编译器产生影响,即任何一个都是正确的.对?

There are lots of tutorials and quesitons addressing this. But I want to confirm my understanding in one specific case. The two below should not make a difference to the compiler i.e either one is correct. Right?

typedef _GridLayoutInputRepeater<_num-1,Figure,_types...> _base;

#define _base _GridLayoutInputRepeater<_num-1,Figure,_types...> 

同样,下面的内容应该没有区别?

Similarly , the below should not make the difference?

#define INT_32 uint32_t

typedef uint32_t INT_32;

跟进线程此处

推荐答案

目前没有显示用例,这两种情况都是相等的",但你应该注意的是 #define 是一个整体不同于 typedef 的野兽.

Currently without showing use-cases the 2 situations are both "equal" but what you should note is that #define is a whole different beast than typedef.

typedef 为另一种类型引入别名,该别名将被编译器看到,因此将遵循编译器规则、作用域等.

typedef introduces an alias for another type, this alias will be seen by the compiler and thus will follow compiler rules, scoping etc.

A #define 是一个预处理器宏,预处理器将在实际编译器之前运行,并且会进行文本替换,它不关心作用域或任何语法规则,它非常愚蠢".

A #define is a preprocessor macro, the preprocessor will run before the actual compiler and will literally do a textual replacement, it does not care about scoping or any syntax rules, it's quite "dumb".

通常,typedefs 是可行的方法,因为它们不容易出错.在这种情况下,您也可以使用 using = ,但这是个人喜好,因为它们都是相同的:

Usually, typedefs are the way to go as they are so much less error-prone. In which case you could use using = as well but that's personal preference since they're both the same:

using _base = _GridLayoutInputRepeater<_num-1,Figure,_types...>;

这篇关于确认对 typedef 和 #define 的理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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