I'm mildly curious how gcc and clang treat "__func__" internally
that leads to this odd behavior. The obvious way to implement it
would be to internally create a declaration of __func__ on entry
to each function definition, which shouldn't cause the symptom
you're seeing. But it's not a conformance issue
Thiago Adams <thiago.adams@gmail.com> writes:
This program does not compile..in gcc and clang
int __func__ i =1;
I'm mildly curious how gcc and clang treat "__func__" internally
that leads to this odd behavior.
source code and its behavior indicate that gcc treats __func__ as
a keyword, which is inconsistent with the info page. For example,
one would expect this:
int main(void) {
{
int __func__;
}
}
to be accepted, with the inner definition of __func__ hiding the
implicit static declaration, but gcc reports a syntax error.
It's not a conformance issue, since __func__ is a reserved identifier
and any code that can tell whether it's a keyword has undefined
behavior.
On 2025-03-16, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:...
It's not a conformance issue, since __func__ is a reserved identifier
and any code that can tell whether it's a keyword has undefined
behavior.
But __func__ is not a reserved identifier!
Are you suggesting that a conforming compiler must accept the above
code (defining __func__ in an inner scope)? If so, I disagree.
Kaz Kylheku <643-408-1753@kylheku.com> writes:
On 2025-03-16, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
Are you suggesting that a conforming compiler must accept the above
code (defining __func__ in an inner scope)? If so, I disagree.
I was initially divided on this one.
On the one hand, inside a function, __func__ is not reserved (for future
use); it is described as being bound as a variable name, and that makes
it compatible with lexical shadowing.
That's where I disagree, though I'm not 100% certain.
Identifiers starting with __ are not conditionally reserved, and
they're not reserved "for future use". They're simply "reserved
for any use".
Kaz Kylheku <643-408-1753@kylheku.com> writes:
On 2025-03-15, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
I'm mildly curious how gcc and clang treat "__func__" internally
that leads to this odd behavior. The obvious way to implement it
would be to internally create a declaration of __func__ on entry
to each function definition, which shouldn't cause the symptom
you're seeing. But it's not a conformance issue
The main reason is this:
"As an extension, at file (or, in C++, namespace scope), __func__
evaluates to the empty string."
But it produces a diagnostic
warning: ?__func__? is not defined outside of function scope
And in a very quick look through the documentation, I don't see
a way to disable that warning (other than "-w", which disables
all warnings). Looks like they don't really want you using this
extension.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 148:50:22 |
Calls: | 10,383 |
Calls today: | 8 |
Files: | 14,054 |
D/L today: |
2 files (1,861K bytes) |
Messages: | 6,417,760 |