gpt4 book ai didi

c++ - __func__ 值在 C 和 C++ 之间的区别

转载 作者:可可西里 更新时间:2023-11-01 17:08:00 25 4
gpt4 key购买 nike

C 标准保证 _ _ func _ _ 值始终是封闭函数的名称,而在 C++ 中(我指的当然是 C++11)它可以是任何实现定义的字符串(例如,如果我们有不带参数的函数 foo,我们可以得到类似于“Some string fdgdg asdfs fsdf sd”)?

ISO/IEC 9899:2011

6.4.2.2 预定义标识符

Semantics

1 The identifier _ _ func _ _ shall be implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration static const char _ func _ [] = "function-name"; appeared, where function-name is the name of the lexically-enclosing function.

ISO/IEC 14882:2011

8.4.1 一般 [dcl.fct.def.general]

8 The function-local predefined variable _ _ func _ _ is defined as if a definition of the form static const char _ _ func _ _ [] = "function-name "; had been provided, where function-name is an implementation-defined string. It is unspecified whether such a variable has an address distinct from that of any other object in the program.

这是什么原因呢?如果我们无法接收当前函数名称,是否返回类似“Unknown”的内容?

最佳答案

C++ 中函数的“名称”是什么?

  • 它是否由其 namespace 限定?
  • 重载函数是否以其参数类型命名?
  • 不用于重载的返回类型呢?
  • 函数模板中发生了什么?
  • 调用约定是名称的一部分吗?
  • 'const' 和 'volatile' 应该写在类型名之前还是之后?

“实现定义的字符串”使编译器可以灵活地将所有这些信息填充到字符串中。它会向您(程序员)发送一条消息,如果您想要可移植性,您不应该尝试执行字符串比较或解析,或者实际上除了记录值之外做任何事情。

(您甚至不能将这些值相互比较,因为不能保证它们是唯一的。似乎所有重载都可能产生相同的字符串。因此不建议使用它来编译分析器统计信息。)

关于c++ - __func__ 值在 C 和 C++ 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14164161/

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