Error: template argument uses local type

G

Gabriel Schreiber

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
 
R

Rolf Magnus

Gabriel said:
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.
 
T

tom_usenet

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top