Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Jim Langston
Guest
 
Posts: n/a
#1: Feb 15 '06
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?



Dietmar Kuehl
Guest
 
Posts: n/a
#2: Feb 15 '06

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


Jim Langston wrote:[color=blue]
> error C2926: 'main::Test' : types with no linkage cannot be used as
> template arguments[/color]
[...][color=blue]
> Is this something with VC++ or part of the standard?[/color]

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.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Kai-Uwe Bux
Guest
 
Posts: n/a
#3: Feb 15 '06

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


Jim Langston wrote:
[color=blue]
> 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?[/color]

"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
Diego Martins
Guest
 
Posts: n/a
#4: Feb 16 '06

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


an idiotic restriction :(

Ben Pope
Guest
 
Posts: n/a
#5: Feb 16 '06

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


Diego Martins wrote:[color=blue]
> an idiotic restriction :([/color]

Perhaps it's to help with export template?

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Bo Persson
Guest
 
Posts: n/a
#6: Feb 16 '06

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



"Diego Martins" <jose.diego@gmail.com> skrev i meddelandet
news:1140115668.739395.111800@g47g2000cwa.googlegr oups.com...[color=blue]
> an idiotic restriction :(
>[/color]

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


Jeff Flinn
Guest
 
Posts: n/a
#7: Feb 16 '06

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


Bo Persson wrote:[color=blue]
> "Diego Martins" <jose.diego@gmail.com> skrev i meddelandet
> news:1140115668.739395.111800@g47g2000cwa.googlegr oups.com...[color=green]
>> an idiotic restriction :(
>>[/color]
>
> 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.[/color]

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

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.
[color=blue]
> So, the Standards Committee said: You just can't do this![/color]

Too bad.
[color=blue]
> Problem solved?[/color]

Probably not. :)

Jeff Flinn



Ben Pope
Guest
 
Posts: n/a
#8: Feb 16 '06

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


Ben Pope wrote:[color=blue]
> Diego Martins wrote:[color=green]
>> an idiotic restriction :([/color]
>
> Perhaps it's to help with export template?[/color]

Ignore that.

I think I mis-recalled what you were referring to.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Bo Persson
Guest
 
Posts: n/a
#9: Feb 16 '06

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



"Jeff Flinn" <NONONE@nowhere.com> skrev i meddelandet
news:dt2ls8$3lv$1@bluegill.adi.com...[color=blue]
> Bo Persson wrote:[/color]
[color=blue][color=green]
>> So, do we want to have two types named std::vector<Test> that are
>> not
>> the same type? Not really.[/color]
>
> 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.[/color]

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
names are globally unique. That means that std::vector<global_name>
will also be unique.
[color=blue]
>[color=green]
>> So, the Standards Committee said: You just can't do this![/color]
>
> Too bad.
>[color=green]
>> Problem solved?[/color]
>
> Probably not. :)
>[/color]

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/wg...2003/n1427.pdf


Bo Persson


Closed Thread