Compile the following code with g++ creates the following errors: template <void (*p)(void)> struct S03 {}; class C03 { public: static void f(void) {} void g(void) { S03<&f> s03; } }; In member function `void C03::g()': error: missing `>' to terminate the template argument list error: template argument 1 is invalid error: expected `;' before "f"
Doing 03<(&f)> s03; or 03<f> s03; instead I get: t.cc:6: error: `static void C03::f()' cannot appear in a constant-expression t.cc:6: error: `&' cannot appear in a constant-expression t.cc:6: error: template argument 1 is invalid t.cc:6: error: invalid type in declaration before ';' token So this is actually invalid code.
From the ANSI-C++ chapter 9.4 Static members: "A static member s of class X may be referred to using the qualified id expression X::s; it is not necessary to use the class member access syntax (5.2.5) to refer to a static member." "A static member may be referred to directly in the scope of its class or in the scope of a class derived (clause 10) from its class"
The correct syntax is void g(void) { S03<&C03::f> s03; } With that it compiles cleanly. W.
According to my quotes from the ANSI-C++ the following syntax is valid: void g(void) { S03<&f> s03; } However, the compiler issues errors. If someone thinks that this is an invalid code, please provide some proof.
I won't confirm it right now, but Tal is right: the standard my not appear clear but the syntax &A::f is used to form pointers to members: a pointer to a static member function is not a pointer to member, it is a regulard pointer to function. [conv.func]/1 (footnote 50) says that "&f" is forbidden for non- static member function, and it implies that "f" is a regular lvalue for a static member function.
Working on a fix.
Subject: Bug 17585 CVSROOT: /cvs/gcc Module name: gcc Changes by: mmitchel@gcc.gnu.org 2004-09-27 14:46:56 Modified files: gcc/cp : ChangeLog semantics.c gcc/testsuite : ChangeLog Added files: gcc/testsuite/g++.dg/template: static8.C Log message: PR c++/17585 * semantics.c (finish_id_expression): Do not add "this->" to static member functions. PR c++/17585 * g++.dg/template/static8.C: New test. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.4387&r2=1.4388 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/semantics.c.diff?cvsroot=gcc&r1=1.438&r2=1.439 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.4356&r2=1.4357 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/template/static8.C.diff?cvsroot=gcc&r1=NONE&r2=1.1
Subject: Bug 17585 CVSROOT: /cvs/gcc Module name: gcc Changes by: mmitchel@gcc.gnu.org 2004-09-27 18:47:30 Modified files: gcc/cp : ChangeLog cp-tree.h search.c semantics.c Log message: PR c++/17585 * cp-tree.h (shared_member_p): Declare. * search.c (shared_member_p): Give it external linkage. * semantics.c (finish_qualified_id_expr): Use it. (finish_id_expression): Likewise. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.4388&r2=1.4389 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/cp-tree.h.diff?cvsroot=gcc&r1=1.1053&r2=1.1054 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/search.c.diff?cvsroot=gcc&r1=1.323&r2=1.324 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/semantics.c.diff?cvsroot=gcc&r1=1.439&r2=1.440
Subject: Bug 17585 CVSROOT: /cvs/gcc Module name: gcc Branch: gcc-3_4-branch Changes by: mmitchel@gcc.gnu.org 2004-09-27 18:50:24 Modified files: gcc/cp : ChangeLog cp-tree.h search.c semantics.c gcc/testsuite : ChangeLog Added files: gcc/testsuite/g++.dg/parse: error18.C gcc/testsuite/g++.dg/template: static8.C Log message: PR c++/17585 * cp-tree.h (shared_member_p): Declare. * search.c (shared_member_p): Give it external linkage. * semantics.c (finish_qualified_id_expr): Use it. (finish_id_expression): Likewise. PR c++/17585 * g++.dg/template/static8.C: New test. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.3892.2.160&r2=1.3892.2.161 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/cp-tree.h.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.946.4.15&r2=1.946.4.16 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/search.c.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.284.4.4&r2=1.284.4.5 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/semantics.c.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.381.4.14&r2=1.381.4.15 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.3389.2.274&r2=1.3389.2.275 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/parse/error18.C.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=NONE&r2=1.1.12.1 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/template/static8.C.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=NONE&r2=1.1.2.1
Fixed in GCC 3.4.3.
*** Bug 17754 has been marked as a duplicate of this bug. ***
Please change the summary to be more descriptive. I just waste time reporting the bug again (as 17754) because I couldn't find this report.