Why can't a local structure be used in a vector?

J

Jim Langston

I was having weird problems in my code so finally I got it down to this,
which doesn't compile:

#include <vector>

int main()
{
struct Test
{
int i;
};

std::vector< Test > MyTest;
}

In VC++ .net 2003 this gives me the error:
error C2926: 'main::Test' : types with no linkage cannot be used as template
arguments

If, however, I declare the structure globally (outside of main) it
compilers.

Is this something with VC++ or part of the standard?
 
K

Kai-Uwe Bux

Jim said:
I was having weird problems in my code so finally I got it down to this,
which doesn't compile:

#include <vector>

int main()
{
struct Test
{
int i;
};

std::vector< Test > MyTest;
}

In VC++ .net 2003 this gives me the error:
error C2926: 'main::Test' : types with no linkage cannot be used as
template arguments

If, however, I declare the structure globally (outside of main) it
compilers.

Is this something with VC++ or part of the standard?

"A local type, a type with no linkage, an unnamed type or a type compounded
from any of these types shall not be used as a template-argument for a
template type-parameter." [14.3.1/2]


Best

Kai-Uwe Bux
 
D

Dietmar Kuehl

Jim said:
error C2926: 'main::Test' : types with no linkage cannot be used as
template arguments [...]
Is this something with VC++ or part of the standard?

This restriction is part of the standard. The reasoning was originally
that local structures don't really have a names and thus it would be
impossible to instantiate a template with them. This reasoning was
based on the compiler technology at this time and approaches are
known now which would allow removal of this restriction. I think it
was discussed to allow local types as template arguments.
 
B

Bo Persson

Diego Martins said:
an idiotic restriction :(

It is, sort of. Like Dietmar explainded, it might be removed in a
future standard.


The original problem is this:

file1.cpp

#include <vector>

void f()
{
struct Test
{ int x; };

std::vector<Test> v1;
}


file2.cpp

#include <vector>

void g()
{
struct Test
{ float y; };

std::vector<Test> v2;

}


Now, are the vectors v1 and v2 of the same type? Not really.

So, do we want to have two types named std::vector<Test> that are not
the same type? Not really.


So, the Standards Committee said: You just can't do this!

Problem solved?


Bo Persson
 
J

Jeff Flinn

Bo said:
It is, sort of. Like Dietmar explainded, it might be removed in a
future standard.


The original problem is this:

file1.cpp

#include <vector>

void f()
{
struct Test
{ int x; };

std::vector<Test> v1;
}


file2.cpp

#include <vector>

void g()
{
struct Test
{ float y; };

std::vector<Test> v2;

}


Now, are the vectors v1 and v2 of the same type? Not really.

As I would expect.
So, do we want to have two types named std::vector<Test> that are not
the same type? Not really.

And I wouldn't expect them to have the same type names. I would expect them
to have names related to the scope that each has.
So, the Standards Committee said: You just can't do this!

Too bad.
Problem solved?

Probably not. :)

Jeff Flinn
 
B

Bo Persson

Jeff Flinn said:
Bo Persson wrote:

And I wouldn't expect them to have the same type names. I would
expect them to have names related to the scope that each has.

At the time, the committee wasn't able to come up with a naming scheme
that would do this. What exactly are the different scopes here? What
if there are other functions f() and g() in other source files, etc?

Global names and names in namespaces have a well defined, and unique
(important!), name. The One Defintion Rule also ensures that these
Too bad.


Probably not. :)

Perhaps there will be another attempt for the next revision of the
standard. At least it is now known that the problem can be solved.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1427.pdf


Bo Persson
 

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,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top