This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Myers' Effective STL "most vexing parse" warning

Item 6 of Myers' "Effective STL" is about code like this simplification of
the g++ test case g++.old-deja/

  struct S {
    S(int b);

  int main(void) {
    double a = 2.0;
    S x(int(a)); // THIS LINE
    return 0;

The fact that the line "S x(int (a));" is actually a local declaration of a
function and not an object instantiation is correct, but it's often
difficult to detect that you've made this error when you were using the
object's constructor & destructor to ensure locking or whatever. I've been
caught out by this, people I work with have been caught out by this, and
other people on the web
( have even reported
this as a g++ bug.

This patch causes g++ to emit a diagnostic if it sees such a thing:

example.cpp: In function `int main()':
example.cpp:8: warning: unused local declaration of function `S x(int)'

This causes various dejagnu test cases to emit additional warnings, but I'm
not sure how to correct the test cases (or indeed whether to say "this
warning is expected" or re-write the test case to avoid the warning). If
someone tells me what to do, I'll make another patch for that.

I don't believe a new test case is necessary for this diagnostic, because
g++.old-deja/ is already explicitly checking that g++
interprets the line in question as a declaration rather than an

I don't know if this should be a pedantic warning or have its own -W option
or whatever; I happily leave that decision up to someone in charge. The
patch below has the warning always enabled.

Thanks for the compiler, everyone!


2004-01-12  Elliott Hughes  <>

        * decl.c (poplevel): Warn about unused local function declarations,
        because they're probably instances of Myers' "most vexing parse"
        the book "Effective STL".

Index: decl.c
RCS file: /cvsroot/gcc/gcc/gcc/cp/decl.c,v
retrieving revision 1.1171
diff -c -3 -p -r1.1171 decl.c
*** decl.c      10 Jan 2004 01:18:08 -0000      1.1171
--- decl.c      12 Jan 2004 17:04:20 -0000
*************** poplevel (int keep, int reverse, int fun
*** 502,507 ****
--- 502,520 ----
      decls = current_binding_level->names;
+   /* Warn about unused local function declarations, because they're
+      probably instances of Myers' "most vexing parse" from
+      the book "Effective STL".  */
+   for (decl = decls; decl; decl = TREE_CHAIN (decl))
+     {
+       if (TREE_CODE (decl) == FUNCTION_DECL
+           && DECL_LOCAL_FUNCTION_P (decl)
+           && !DECL_FRIEND_P (decl) /* Otherwise f is considered local in
+                                     * "struct A { friend void f (); };"
+           && !TREE_USED (decl))
+         cp_warning_at ("unused local declaration of function `%D'", decl);
+     }
    /* Output any nested inline functions within this block
       if they weren't already output.  */
    for (decl = decls; decl; decl = TREE_CHAIN (decl))

This e-mail and any attachment is confidential. It may only be read, copied and used by the intended recipient(s). If you are not the intended recipient(s), you may not copy, use, distribute, forward, store or disclose this e-mail or any attachment. If you are not the intended recipient(s) or have otherwise received this e-mail in error, you should destroy it and any attachment and notify the sender by reply e-mail or send a message to

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]