Bug 7703 - [DR 325] Error parsing a default argument containg a non-parenthesized comma as part of a template-id
Summary: [DR 325] Error parsing a default argument containg a non-parenthesized comma ...
Status: CLOSED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.2
: P3 normal
Target Milestone: ---
Assignee: Wolfgang Bangerth
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-08-23 07:06 UTC by fizban
Modified: 2005-07-23 22:46 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2003-04-30 00:00:00


Attachments
main_s_ii.tar (1.11 KB, application/octet-stream)
2003-05-21 15:16 UTC, fizban
Details

Note You need to log in before you can comment on or make changes to this bug.
Description fizban 2002-08-23 07:06:00 UTC
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;
};
Comment 1 fizban 2002-08-23 07:06:00 UTC
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.
Comment 2 Kriang Lerdsuwanakij 2002-09-13 09:43:59 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: Confirmed.  To be fixed in the new parser.
Comment 3 Giovanni Bajo 2003-05-02 01:22:06 UTC
State-Changed-From-To: analyzed->suspended
State-Changed-Why: Suspended until DR325 is resolved.
Comment 4 Wolfgang Bangerth 2003-05-02 01:26:34 UTC
*** This bug has been marked as a duplicate of 57 ***
Comment 5 Giovanni Bajo 2003-05-02 03:19:25 UTC
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
 
Comment 6 Nathanael C. Nerode 2003-05-23 06:28:57 UTC
yep, it's a dupe all right.