473,325 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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

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?
Feb 15 '06 #1
8 2863
Jim Langston wrote:
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.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Feb 15 '06 #2
Jim Langston wrote:
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
Feb 15 '06 #3
an idiotic restriction :(

Feb 16 '06 #4
Diego Martins wrote:
an idiotic restriction :(


Perhaps it's to help with export template?

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 16 '06 #5

"Diego Martins" <jo********@gmail.com> skrev i meddelandet
news:11**********************@g47g2000cwa.googlegr oups.com...
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
Feb 16 '06 #6
Bo Persson wrote:
"Diego Martins" <jo********@gmail.com> skrev i meddelandet
news:11**********************@g47g2000cwa.googlegr oups.com...
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.


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

Feb 16 '06 #7
Ben Pope wrote:
Diego Martins wrote:
an idiotic restriction :(


Perhaps it's to help with export template?


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...
Feb 16 '06 #8

"Jeff Flinn" <NO****@nowhere.com> skrev i meddelandet
news:dt**********@bluegill.adi.com...
Bo Persson wrote:
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.


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


Too bad.
Problem solved?


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/wg...2003/n1427.pdf
Bo Persson
Feb 16 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John | last post by:
Thanks. If I define the following structure, typedef struct{ vector<int> v; int ID; double time; }node; What should be the size of the structure node? If I insert elements into vector v,...
3
by: Daniele | last post by:
Hi, My problem is the manage of a dynamic vector in a list of structures. In the .hpp file I define the struct and the vector v. typedef struct Word { char* Name; vector<int> v; Word* Next;...
11
by: Richard Thompson | last post by:
I've got a memory overwrite problem, and it looks as if a vector has been moved, even though I haven't inserted or deleted any elements in it. Is this possible? In other words, are there any...
4
by: steflhermitte | last post by:
Dear cpp-ians, I am working with a structure struct segment { .... vector <meta_segment>::iterator it_Z; .... };
1
by: Vince | last post by:
Hi, I would like to associate a couple of value(SFID and nRecNo) with a byte array. So I tried the following structure : typedef std::vector<BYTE> ByteArray; class CardIndex {
7
by: markww | last post by:
Hi, I have a data structure that looks like this: struct MY_STRUCT { int x,y,z; string str1; string str2; vector<floatvfloats1; vector<floatvfloats2;
6
by: pj | last post by:
Hi, I 'm currently writing a program that performs transliteration (i.e., converts greek text written using the english alphabet to "pure" greek text using the greek alphabet) as part of my...
23
by: deepakvsoni | last post by:
Why does C++ restrict definition of functions with in functions? int f1() { int f2() { ..... } } //This is not supported. Thanks in advance to anybody who replies..
2
by: mitmaxtor | last post by:
Hi everyone, I am working on to implement Matlab sort function in C++ and I used the following code. But, it seems it does not work for me. Any idea about how to modify the code or any idea to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.