When compiling the code, I get this error from gcc: main.cpp:22: parse error before `>' token main.cpp:24: syntax error before `.' token main.cpp: In function `int main(int, char**)': main.cpp:31: no matching function for call to `registrar<A, A, int>::registrar(int)' main.cpp:20: candidates are: registrar<A, A, int>::registrar() main.cpp:20: registrar<A, A, int>::registrar(const registrar<A, A, int>&) The default value in the registrar constructor declaration seems to cause a parsing error. My guess is that the multiple levels of templates is causing confusion here. The workaround simplifies this, creating only one level of templates by typedefing the inner template. Release: gcc version 3.2 Environment: Windows 2000 cygwin version 2.125.2.10 gcc configured with options: --prefix=/usr/local/gcc-3.2 How-To-Repeat: template<class object_t> class singleton { public: static object_t& instance() { static object_t object; return object; } }; template<class object_t, class key_t = int> class registry { public: void add(key_t p_key, object_t& p_object) { // do stuff here } }; template<class object_t, class object_base_t = object_t, class key_t = int> class registrar { public: registrar(key_t p_key, registry<object_base_t, key_t>& p_registry = singleton<registry<object_base_t, key_t> >::instance()) { object_t object; p_registry.add(p_key, object); } }; class A { int i; }; int main(int argc, char **argv) { registrar<A> a(1); return 0; };
Fix: Replace class registrar with: template<class object_t, class object_base_t = object_t, class key_t = int> class registrar { public: typedef registry<object_base_t, key_t> REGISTRY; registrar(key_t p_key, REGISTRY& p_registry = singleton<REGISTRY>::instance()) { object_t object; p_registry.add(p_key, object); } }; By "simplifying" the parameters with the typedef, the code compiles fine.
State-Changed-From-To: open->analyzed State-Changed-Why: Confirmed. To be fixed in the new parser.
State-Changed-From-To: analyzed->suspended State-Changed-Why: Suspended until DR325 is resolved.
*** This bug has been marked as a duplicate of 57 ***
From: "Giovanni Bajo" <giovannibajo@libero.it> To: <gcc-gnats@gcc.gnu.org>, <gcc-bugs@gcc.gnu.org>, <nobody@gcc.gnu.org>, <gcc-prs@gcc.gnu.org>, <fizban@umich.edu> Cc: Subject: Re: c++/7703: [2003-01-28] gcc gives parse errors when compiling a class function with recursively templated default value for a function parameter Date: Fri, 2 May 2003 03:19:25 +0200 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=7703 This is only about the comma in the default parameter: ----------------------------- template <class, class> struct Bar {}; template <class> struct Foo { void func(Bar<void, void> b = Bar<void, void>()) { } }; template struct Foo<void>; ----------------------------- pr7703.cpp:8: error: variable or field `func' declared void pr7703.cpp:8: error: expected function-definition I'm confident this is a duplicate but I could not find the other PR. The obvious workaround is just to put a couple of parenthesis around the default parameter to disambiguate the parser (which is confused by the comma). Since this is one of the points discussed in DR325, I suspend this PR till the resolution of the defect report. Giovanni Bajo
yep, it's a dupe all right.