template parameter can't be a locally-declared type?

P

Phil Endecott

Dear Experts,

It seems that I'm not allowed to use a locally-declared type as an
implicit template parameter. Example:

struct Foo {
template <typename T>
Foo(const T& t) {}
};

void f() {
struct {
int a;
} s;
Foo x(s); // doesn't work; "no matching function"
}

void g() {
struct S {
int a;
};
S s;
Foo x(s); // also doesn't work, so it's the local scope not
// being anonymous that's the problem
}

struct stru {
int a;
};
void h() {
stru s;
Foo x(s); // does work.
}


This surprises me. Is this right? What's the rationale?


Cheers, Phil.
 
J

James Kanze

It seems that I'm not allowed to use a locally-declared type
as an implicit template parameter.

Nor as an explicit one. Instantiation types (and addresses)
must have external binding.

[...]
This surprises me. Is this right? What's the rationale?

Probably implementation problems. Unless the binding is
external, the definition isn't available in other translation
units. And orginaly, there was no requirement for the template
code to be available when you compiled---template instantiations
were always in another translation unit.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top