public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/50479] New: Unevaluated usage of parameters in function default arguments is accepted
@ 2011-09-22  9:26 daniel.kruegler at googlemail dot com
  2024-04-04  6:56 ` [Bug c++/50479] " pinskia at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: daniel.kruegler at googlemail dot com @ 2011-09-22  9:26 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50479

             Bug #: 50479
           Summary: Unevaluated usage of parameters in function default
                    arguments is accepted
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: daniel.kruegler@googlemail.com
                CC: jason@redhat.com


gcc 4.7.0 20110917 (experimental) accepts the following program with or without
-Wall, -pedantic, or -std=c++0x:

//---
long foo;
void bar(char foo = sizeof(foo)) {}

int main() {
  bar();
}
//---

But according to 3.4.1 p11:

"During the lookup for a name used as a default argument (8.3.6) in a function
parameter-declaration-clause or used in the expression of a mem-initializer for
a constructor (12.6.2), the function parameter names are visible and hide the
names of entities declared in the block, class or namespace scopes containing
the function declaration."

and to 8.3.6 p9:

"Consequently, parameters of a function shall not be used in a default
argument,
even if they are not evaluated. Parameters of a function declared before a
default argument are in scope and can hide namespace and class member names."

[The references refer to N3190, but also apply to C++03, 14882:2003(E)]

the name 'foo' in the expression 'sizeof(foo)' should resolve to the parameter
name and after this should make the program ill-formed. Refining this program
to

//---
extern "C" int printf(const char*, ...);

long foo;
void bar(char foo = sizeof(foo)) {
  int v = foo;
  printf("%d", v);
}

int main() {
  bar();
}
//---

produces the output

4

leading to the conclusion that the name 'foo' is resolved to the name of the
global variable, not to the parameter name.

A similar example can be constructed via decltype and compiled successfully
with -std=c++0x:

//---
template<class>
struct A;

template<>
struct A<long> {
  static const int value = 1;
};

long foo;

void bar(char foo = A<decltype(foo)>::value) {}

int main() {
  bar();
}
//---

This example should also be rejected for similar reasons as above, because the
name 'foo' in the default argument should resolve to the parameter, but again
the global variable name is found instead.


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/50479] Unevaluated usage of parameters in function default arguments is accepted
  2011-09-22  9:26 [Bug c++/50479] New: Unevaluated usage of parameters in function default arguments is accepted daniel.kruegler at googlemail dot com
@ 2024-04-04  6:56 ` pinskia at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-04  6:56 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=50479

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |fchelnokov at gmail dot com

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 102635 has been marked as a duplicate of this bug. ***

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-04-04  6:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-22  9:26 [Bug c++/50479] New: Unevaluated usage of parameters in function default arguments is accepted daniel.kruegler at googlemail dot com
2024-04-04  6:56 ` [Bug c++/50479] " pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).