Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Error: template argument uses local type (http://www.velocityreviews.com/forums/t279560-error-template-argument-uses-local-type.html)

Gabriel Schreiber 11-27-2003 11:35 AM

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

Rolf Magnus 11-27-2003 11:50 AM

Re: Error: template argument uses local type
 
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.


tom_usenet 11-27-2003 12:48 PM

Re: Error: template argument uses local type
 
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/d...2003/n1427.pdf

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html


All times are GMT. The time now is 01:11 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.