gpt4 book ai didi

c++ - 为什么 __func__、__FUNCTION__ 和 __PRETTY_FUNCTION__ 不是预处理器宏?

转载 作者:IT老高 更新时间:2023-10-28 12:51:15 25 4
gpt4 key购买 nike

我刚刚注意到 __func____FUNCTION____PRETTY_FUNCTION__ 不被视为预处理器宏,并且在16.8 预定义宏名称 标准 (N4527 Working Draft) 部分。

这意味着它们不能用于 phase 6 的字符串连接技巧。 :

// Valid
constexpr char timestamp[]{__FILE__ " has been compiled: " __DATE__ " " __TIME__};
// Not valid!!!
template <typename T>
void die() { throw std::runtime_error{"Error detected in " __PRETTY_FUNCTION__}; }

据我所知,__FILE____DATE____TIME__ 被翻译为标准规定的字符串文字:

16.8 Predefined macro names [cpp.predefined]

__DATE__

The date of translation of the source file: a character string literal of the form "Mmm dd yyyy", where the names of the months are the same as those generated by the asctime function, and the first character of dd is a space character if the value is less than 10. If the date of translation is not available, an implementation-defined valid date shall be supplied.

__FILE__

The presumed name of the current source file (a character string literal).

__TIME__

The time of translation of the source file: a character string literal of the form "hh:mm:ss" as in the time generated by the asctime function.

__func__ 被标准称为函数局部预定义变量,形式如下:

static const char __func__[] = "function-name ";

所以事实是这是一个局部变量,因此字符串连接技巧不适用于它。

至于 __FUNCTION____PRETTY_FUNCTION__ 并没有在标准中提及(是否定义了实现?)但是认为它们的行为会像 __func__.

所以问题是:为什么 __func____FUNCTION____PRETTY_FUNCTION__ 是函数局部静态常量数组,而 __FILE____DATE____TIME__ 是字符串文字吗?这个决定背后的理由是什么(如果有的话)?

最佳答案

在预处理时扩展 __func__ 要求预处理器知道它正在处理哪个函数。预处理器通常不知道这一点,因为解析发生在预处理器完成之后。

一些实现结合了预处理和解析,在这些实现中,__func__ 可以按照您希望的方式工作。事实上,如果我没记错的话,MSVC 的 __FUNCTION__ 就是这样工作的。但是,将翻译阶段分开是对实现的不合理要求。

关于c++ - 为什么 __func__、__FUNCTION__ 和 __PRETTY_FUNCTION__ 不是预处理器宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31430765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com