gpt4 book ai didi

c++ - __func__ 外部函数定义

转载 作者:IT老高 更新时间:2023-10-28 21:46:37 24 4
gpt4 key购买 nike

如果我们在 C (C99/C11) 和 C++ 中的函数之外使用预定义变量 __func__ 会发生什么?

#include <stdio.h>

const char* str = __func__;

int main(void)
{
printf("%s", str);
return 0;
}

gcc 4.7.2 只给出警告(启用 -Wall -W -pedantic)并且什么也不打印。

标准没有明确说明:

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.

ISO/IEC 9899:2011

6.4.2.2 预定义标识符

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.

UB?错误?还是别的什么?

最佳答案

Standard doesn't say anything about it explicitly

这意味着未定义的行为。

来自 C 标准(重点是我的):

(C99, 4.p2) "If a ‘‘shall’’ or ‘‘shall not’’ requirement that appears outside of a constraint is violated, the behavior is undefined. Undefined behavior is otherwise indicated in this International Standard by the words ‘‘undefined behavior’’ or by the omission of any explicit definition of behavior. There is no difference in emphasis among these three; they all describe ‘‘behavior that is undefined’’."

关于c++ - __func__ 外部函数定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14123347/

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