Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Error: template argument uses local type

Reply
Thread Tools

Error: template argument uses local type

 
 
Gabriel Schreiber
Guest
Posts: n/a
 
      11-27-2003
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
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      11-27-2003
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.

 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      11-27-2003
On Thu, 27 Nov 2003 12:35:34 +0100, Gabriel Schreiber
<(E-Mail Removed)-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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class nested inside a template class as template function argument type claudiu C Programming 2 04-01-2011 12:10 PM
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
Template argument as template argument nw C++ 0 04-14-2008 01:36 PM
local type as template argument Filip Konvicka C++ 2 05-22-2007 08:43 AM
Design issue : "self type" as a default template argument (recursive template arguments) IR C++ 3 11-22-2006 08:38 PM



Advertisments