Error: template argument uses local type



 DEVELOP > c-Plus-Plus > Error: template argument uses local type

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Gabriel Schreiber"
Date: 27 Nov 2003 05:35:34 AM
Object: Error: template argument uses local type
Hi
this code:
#include <vector>
void f()
{
struct arg{};
std::vector<arg> v;
};
gets the error message:
: template-argument `f()::arg' uses local type `f()::arg'
: template argument 2 is invalid
can anyone explain this?
And: Is it possible in any way to instantiate templates with local
types?
TIA
Gabriel
.

User: "Rolf Magnus"

Title: Re: Error: template argument uses local type 27 Nov 2003 05:50:42 AM
Gabriel Schreiber wrote:

Hi

this code:

#include <vector>
void f()
{
struct arg{};
std::vector<arg> v;
};

gets the error message:
: template-argument `f()::arg' uses local type `f()::arg'
: template argument 2 is invalid

can anyone explain this?

The compiler already said it. You're using a local type as template
argument, and that's not allowed in c++. Put the struct definition
ouside the function, and it should work.

And: Is it possible in any way to instantiate templates with local
types?

No.
.

User: "tom_usenet"

Title: Re: Error: template argument uses local type 27 Nov 2003 06:48:08 AM
On Thu, 27 Nov 2003 12:35:34 +0100, Gabriel Schreiber
<schreiber@ient.rwth-aachen.de> wrote:

Hi

this code:

#include <vector>
void f()
{
struct arg{};
std::vector<arg> v;
};

gets the error message:
: template-argument `f()::arg' uses local type `f()::arg'
: template argument 2 is invalid

can anyone explain this?

And: Is it possible in any way to instantiate templates with local
types?

No, the problem is that template arguments must have external linkage,
but local types have no linkage. You can move the struct to an
anonymous namespace to work around the problem without polluting a
namespace with your type.
You might want to read this:
http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1427.pdf
Tom
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER