This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Using local struct with templates
- From: Ian Lance Taylor <iant at google dot com>
- To: dinonet at gmx dot de
- Cc: gcc-help at gcc dot gnu dot org
- Date: Fri, 04 Jun 2010 08:52:06 -0700
- Subject: Re: Using local struct with templates
- References: <4C07BCA2.2080700@gmx.de>
dinonet@gmx.de writes:
> Hi, I've got a problem with using a template together with a struct
> declared inside a function body, see example below. When I compile
> "g++ -D TEST1 test.cpp" or "g++ -D TEST2 test.cpp" the compiler
> answers with "error: no matching function for call to
> Buffer:read()'". "g++ -D TEST3 test.cpp" compiles and runs as expected
> but is using a globally defined struct.
>
> Can someone tell me if this compiler error is a bug in the compiler or
> something not covered by the C++ standard? Is there any other
> workaround besides making the struct global?
The compiler is correct. The C++98 standard forbids instantiating a
template with a local type. This restriction is lifted in the draft
C++0x standard, and indeed current versions of gcc do compile your
code with the -std=c++0x option.
Ian