473,387 Members | 1,603 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,387 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 2869
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.