Alias for a std::vector

P

Peter Olcott

I am trying to refer to the same std::vector in a class by two different names,
I tried a union, and I tried a reference, I can't seem to get the syntax right.
Can anyone please help? Thanks
 
V

Victor Bazarov

Peter said:
I am trying to refer to the same std::vector in a class by two
different names, I tried a union, and I tried a reference, I can't
seem to get the syntax right. Can anyone please help? Thanks

See FAQ 5.8.

V
 
P

Peter Olcott

struct Test {
union {
std::vector<int> Fred;
std::vector<int> Charlie;
};
};


struct Test2 {
union {
std::vector<int> Fred;
std::vector<int>& Charlie = Fred;
};
};

Neither of these two compile
 
J

Jim Langston

Peter Olcott said:
struct Test {
union {
std::vector<int> Fred;
std::vector<int> Charlie;
};
};


struct Test2 {
union {
std::vector<int> Fred;
std::vector<int>& Charlie = Fred;
};
};

Neither of these two compile

Please don't top post. I fixed the order of your post.

std::vector is a container. You can only use POD (Plain Old Data) in
unions, not classes or (probably) templates.

If you want Charlie to be an alias to Fred, make it a reference.

std::vector<int> Fred;
std::vector<int>& Charlie = Fred;
 
P

Peter Olcott

Jim Langston said:
Please don't top post. I fixed the order of your post.

std::vector is a container. You can only use POD (Plain Old Data) in unions,
not classes or (probably) templates.

If you want Charlie to be an alias to Fred, make it a reference.

std::vector<int> Fred;
std::vector<int>& Charlie = Fred;

My second example above tries this, and it does not compile.
 
P

Peter Olcott

Ian Collins said:
No it doesn't, you still have them in a union.


struct Test2 {
std::vector<int> Fred;
std::vector<int>& Charlie = Fred;
};

That was just a cut-and-paste error. The above code does not compile.
 
P

Peter Olcott

Ian Collins said:
No it doesn't, you still have them in a union.

Here is the whole program and the error messages:

#include <vector>

struct Test2 {
std::vector<int> Fred;
std::vector<int>& Charlie = Fred;
};

int main() {
}


O:\cpp>set echo on

O:\cpp>cl -GX -O2 t.cpp 1>Error

O:\cpp>type Error
t.cpp
T.CPP(7) : error C2327: 'Test2::Fred' : member from enclosing class is not a
type name, static, or enumerator
T.CPP(7) : error C2065: 'Fred' : undeclared identifier
T.CPP(7) : error C2864: 'Charlie' : only const static integral data members can
be initialized inside a class or struct
 
S

Salt_Peter

Peter said:
My second example above tries this, and it does not compile.

As was explained, the second example implements a union with the
afore-mentioned consequences.

#include <iostream>
#include <ostream>
#include <vector>

struct Test
{
std::vector<int> Fred;
std::vector<int>& Charlie;
public:
Test() : Fred(), Charlie(Fred) { }
~Test() { }
void getLocations() const
{
std::cout << "&Fred = " << &Fred;
std::cout << "\n&Charlie = " << &Charlie;
std::cout << std::endl;
}
};

int main()
{
Test test;
test.getLocations();
return 0;
}

/*
&Fred = 006BFDD8
&Charlie = 006BFDD8
*/
 
P

Peter Olcott

Salt_Peter said:
As was explained, the second example implements a union with the
afore-mentioned consequences.

#include <iostream>
#include <ostream>
#include <vector>

struct Test
{
std::vector<int> Fred;
std::vector<int>& Charlie;
public:
Test() : Fred(), Charlie(Fred) { }
~Test() { }
void getLocations() const
{
std::cout << "&Fred = " << &Fred;
std::cout << "\n&Charlie = " << &Charlie;
std::cout << std::endl;
}
};

int main()
{
Test test;
test.getLocations();
return 0;
}

/*
&Fred = 006BFDD8
&Charlie = 006BFDD8
*/

That was simply a cut-and-paste error. I posted the whole program and the
compiler error messages on a previous post.
 
V

Victor Bazarov

Peter said:
struct Test2 {
std::vector<int> Fred;
std::vector<int>& Charlie = Fred;
};

That was just a cut-and-paste error. The above code does not compile.

Correct. Declarations of members shall not have any initialisation
unless they are of static integral constants. All initialisation has
to go to the constructor initialiser list.

V
 
V

Victor Bazarov

Peter said:
Here is the whole program and the error messages:

#include <vector>

struct Test2 {
std::vector<int> Fred;
std::vector<int>& Charlie = Fred;

Drop the "= Fred" portion, and add here:

Test2() : Charlie(Fred) {}
};

int main() {
}


O:\cpp>set echo on

O:\cpp>cl -GX -O2 t.cpp 1>Error

O:\cpp>type Error
t.cpp
T.CPP(7) : error C2327: 'Test2::Fred' : member from enclosing class
is not a type name, static, or enumerator
T.CPP(7) : error C2065: 'Fred' : undeclared identifier
T.CPP(7) : error C2864: 'Charlie' : only const static integral data
members can be initialized inside a class or struct

V
 
I

Ian Collins

Peter said:
Here is the whole program and the error messages:

#include <vector>

struct Test2 {
std::vector<int> Fred;
std::vector<int>& Charlie = Fred;
};

int main() {
}

As shown else thread, you can't do this, a minimum solution is

struct Test2 {
std::vector<int> Fred;
std::vector<int>& Charlie;

Test2() : Charlie(Fred) {}
};
 
P

Peter Olcott

Victor Bazarov said:
Correct. Declarations of members shall not have any initialisation
unless they are of static integral constants. All initialisation has
to go to the constructor initialiser list.

V
Ah that seems to be right, I will try it.
 
J

Jerry Coffin

[ ... ]
My second example above tries this, and it does not compile.

This is still trying to put a vector<int> inside of a union, and (as
already mentioned) you can only put PODs into unions. There's also the
minor detail that in-place initialization like you've tried to use above
can be used with things like static const variables, or with
enumerators, but not with normal data members. Try something like this:

struct Test3 {
std::vector<int> Fred;
std::vector<int> &Charlie;

Test3() : Charlie(Fred) {}
};

....and there's a good chance your compiler will be happier.
 
P

Peter Olcott

Victor Bazarov said:
Drop the "= Fred" portion, and add here:

Test2() : Charlie(Fred) {}

The basic idea works, yet I don't know the required syntax for all of my other
constructors.
What is the syntax for an initializer list when the constructor has parameters?
 
J

Jim Langston

Peter Olcott said:
The basic idea works, yet I don't know the required syntax for all of my
other constructors.
What is the syntax for an initializer list when the constructor has
parameters?

Test2( int SomeInt, std::string SomeString ): Charlie( Fred ), OtherVar(
SomeInt ) { /*...*/ };
 
V

Victor Bazarov

Peter said:
[..]
The basic idea works, yet I don't know the required syntax for all of
my other constructors.
What is the syntax for an initializer list when the constructor has
parameters?

What book are you reading that doesn't describe parameterized c-tors
and their initialiser lists????

V
 

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,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top