constructing const vectors

D

Dan Smithers

Is there a way of specifying the contents of a vector in the constructor
when the elements are not identical?

I would like to be able to construct a static const vector that contains
pre-determined values.

class CFoo
{
static const std::vector<int> m_foo;

static const int *const m_bar;
....
};

const std::vector<int> CFoo::m_foo({1, 2, 3});
const int *const CFoo::m_bar = {10, 20, 30};

As I want a static member, it needs to be all done through the constructor.

Would it be easier just to declare a const array as in m_bar?

thanks

dan
 
L

Lionel B

Is there a way of specifying the contents of a vector in the constructor
when the elements are not identical?

I would like to be able to construct a static const vector that contains
pre-determined values.

class CFoo
{
static const std::vector<int> m_foo;

static const int *const m_bar;
...
};

const std::vector<int> CFoo::m_foo({1, 2, 3});
const int *const CFoo::m_bar = {10, 20, 30};

As I want a static member, it needs to be all done through the
constructor.

std::vector has a constructor that takes a range (i.e. a first and last
iterator) as parameters. These iterators can be pointers, so I suppose
you could use that. Eg.:

#include<vector>

int main()
{
const int a[] = {10,20,30};
const int* const p = a;
const std::vector<int> v(p,p+3);
}

works as expected.
Would it be easier just to declare a const array as in m_bar?

Only you know the answer to that.
 
C

Christian Hackl

Dan said:
Is there a way of specifying the contents of a vector in the constructor
when the elements are not identical?

You could use the Boost Assignment library.

http://www.boost.org/doc/libs/1_35_0/libs/assign/doc/index.html
I would like to be able to construct a static const vector that contains
pre-determined values.

class CFoo
{
static const std::vector<int> m_foo;

static const int *const m_bar;
...
};

const std::vector<int> CFoo::m_foo({1, 2, 3});
const int *const CFoo::m_bar = {10, 20, 30};

const std::vector<int> CFoo::m_foo(list_of(1)(2)(3));
 
R

Rolf Magnus

Lionel said:
std::vector has a constructor that takes a range (i.e. a first and last
iterator) as parameters. These iterators can be pointers, so I suppose
you could use that. Eg.:

#include<vector>

int main()
{
const int a[] = {10,20,30};
const int* const p = a;
const std::vector<int> v(p,p+3);
}

works as expected.

Or a bit simpler:

#include<vector>

int main()
{
const int a[] = {10,20,30};
const std::vector<int> v(a, a + sizeof(a));
}
 
M

Marcel Müller

Rolf said:
Or a bit simpler:

#include<vector>

int main()
{
const int a[] = {10,20,30};
const std::vector<int> v(a, a + sizeof(a));
}

That's wrong.

You wanted to write

const std::vector<int> v(a, a + sizeof(a)/sizeof(*a));


Marcel
 
J

James Kanze

Rolf Magnus schrieb:
Or a bit simpler:
#include<vector>
int main()
{
const int a[] = {10,20,30};
const std::vector<int> v(a, a + sizeof(a));
}
That's wrong.
You wanted to write
const std::vector<int> v(a, a + sizeof(a)/sizeof(*a));

Actually, what he probably wanted to write was:

std::vector< int > const v( begin(a), end(a) ) ;

where begin() and end() are the usual thing that everyone pretty
much has in their toolkit:

template< typename T, size_t N >
T*
begin( T (&array)[ N ] )
{
return array ;
}

template< typename T, size_t N >
T*
end( T (&array)[ N ] )
{
return array + N ;
}
 
C

Christian Hackl

Lionel said:
error: call of overloaded ‘vector(boost::assign_detail::generic_list<int>&)’ is ambiguous

but:

const std::vector<int> CFoo::m_foo = list_of(1)(2)(3);

seems ok. I don't understand why - I'd have thought they were equivalent.

Well, they are not exactly equivalent. In the second version, a
temporary std::vector is constructed using list_of(1)(2)(3), and m_foo
is then initialised with that temporary using std::vector's copy
constructor.

So it seems to me that in this example, construction of said temporary
std::vector is necessary to make list_of work, and that the compiler
does not elide the copy constructor call (as it usually does) because
that would change the meaning of the code.

Someone care to shed some light on this issue? :)
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top