Using local struct with templates
Ian Lance Taylor
Fri Jun 4 16:55:00 GMT 2010
> 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.
More information about the Gcc-help