Address of vector::operator[n]() if instantiated with C-array type 
Author Message
 Address of vector::operator[n]() if instantiated with C-array type

This compiles & works:

std::vector<int> v;
int* p = &v[0];

but this doesn't:

vector<int [5]> v;
int (*p)[5] = &v[0]; // < - error C2102: '&' requires l-value

There's certainly no reason I can see why this should be an error (nor
any other compiler I can find with the same problem), so I assume it's
a bug in VC?
One work around is

int (&r)[5] = v[0];
int (*p)[5] = &r;

Same problem occurs when using const.  typedef's don't help (I had a
typedef for int [5] at first).

Dylan



Tue, 18 May 2004 13:39:27 GMT  
 Address of vector::operator[n]() if instantiated with C-array type

Quote:

> This compiles & works:

> std::vector<int> v;
> int* p = &v[0];

> but this doesn't:

> vector<int [5]> v;
> int (*p)[5] = &v[0]; // < - error C2102: '&' requires l-value

> There's certainly no reason I can see why this should be an error (nor
> any other compiler I can find with the same problem), so I assume it's
> a bug in VC?
> One work around is

> int (&r)[5] = v[0];
> int (*p)[5] = &r;

> Same problem occurs when using const.  typedef's don't help (I had a
> typedef for int [5] at first).

The error doesn't seem to relate to the real problem:  the type of the
objects stored in a Standard Library Container type must be
Assignable.  It is not possible to assign to either an array or a
const-qualified type.  Compilers typically handle these problems in
non-obvious ways, leading to bizarre and not-very-helpful error types.

In summary:

  vector<int [5]> v

and

  vector<const int> v

are both illegal types, as neither type is defined to hold Assignable
types.

HTH,
Micah



Wed, 19 May 2004 04:33:48 GMT  
 Address of vector::operator[n]() if instantiated with C-array type

Quote:


> > This compiles & works:

> > std::vector<int> v;
> > int* p = &v[0];

> > but this doesn't:

> > vector<int [5]> v;
> > int (*p)[5] = &v[0]; // < - error C2102: '&' requires l-value

> > There's certainly no reason I can see why this should be an error (nor
> > any other compiler I can find with the same problem), so I assume it's
> > a bug in VC?
> > One work around is

> > int (&r)[5] = v[0];
> > int (*p)[5] = &r;

> > Same problem occurs when using const.  typedef's don't help (I had a
> > typedef for int [5] at first).

> The error doesn't seem to relate to the real problem:  the type of the
> objects stored in a Standard Library Container type must be
> Assignable.  It is not possible to assign to either an array or a
> const-qualified type.  Compilers typically handle these problems in
> non-obvious ways, leading to bizarre and not-very-helpful error types.

> In summary:

>   vector<int [5]> v

> and

>   vector<const int> v

> are both illegal types, as neither type is defined to hold Assignable
> types.

Damn, of course...there wouldn't be much you could do with a vector
like this, almost any non-const operation to it would require
assigning to an element, which would be impossible.  That's a big
nuisance though, I kinda needed it for what I'm trying to do (where
there are two potential sources of data, one a static fixed-size
table, and alternatively generated on the fly)  Sounds like a rare
case where it's almost impossible to avoid having to manaully new []
and delete []...

Dylan



Wed, 19 May 2004 12:38:40 GMT  
 Address of vector::operator[n]() if instantiated with C-array type

Quote:

>but this doesn't:

>vector<int [5]> v;

<wrinkles nose> Ugh.  Can't you use vectors of size 5 as the elements?  
Something like this:

vector<vector<int> > v;

vector<int> item(5);
// put stuff in 'item'...
v.push_back(item);

--

Dept. of Physics and Computer Science        Clinton, South Carolina USA



Wed, 19 May 2004 13:37:57 GMT  
 Address of vector::operator[n]() if instantiated with C-array type


Quote:

> > In summary:

> >   vector<int [5]> v

> > and

> >   vector<const int> v

> > are both illegal types, as neither type is defined to hold Assignable
> > types.

> Damn, of course...there wouldn't be much you could do with a vector
> like this, almost any non-const operation to it would require
> assigning to an element, which would be impossible.  That's a big
> nuisance though, I kinda needed it for what I'm trying to do (where
> there are two potential sources of data, one a static fixed-size
> table, and alternatively generated on the fly)  Sounds like a rare
> case where it's almost impossible to avoid having to manaully new []
> and delete []...

Any reason:

struct fiveInt {
  int data[5];
  int& operator[](int i) {return data[i];}

Quote:
};

vector<fiveInt> v;

won't work?  fiveInt is Assignmable...You could even make a implicit
constructor from a 5 element int array, if you don't control the structure
of your data source.

Ken



Wed, 19 May 2004 14:59:22 GMT  
 Address of vector::operator[n]() if instantiated with C-array type

Quote:




> > > In summary:

> > >   vector<int [5]> v

> > > and

> > >   vector<const int> v

> > > are both illegal types, as neither type is defined to hold Assignable
> > > types.

> > Damn, of course...there wouldn't be much you could do with a vector
> > like this, almost any non-const operation to it would require
> > assigning to an element, which would be impossible.  That's a big
> > nuisance though, I kinda needed it for what I'm trying to do (where
> > there are two potential sources of data, one a static fixed-size
> > table, and alternatively generated on the fly)  Sounds like a rare
> > case where it's almost impossible to avoid having to manaully new []
> > and delete []...

> Any reason:

> struct fiveInt {
>   int data[5];
>   int& operator[](int i) {return data[i];}
> };

> vector<fiveInt> v;

> won't work?  fiveInt is Assignmable...You could even make a implicit
> constructor from a 5 element int array, if you don't control the structure
> of your data source.

Yes because the pointer used to access the data would be of the wrong
type. Of course you could force it to be the right one, but that
hardly seems an elegant solution.

Just to clarify.  What I'm trying to do is:

typedef int array[5];

array table[10];
vector<array> vec;

const array* data = use_table() ? table : &vec[0];

Would in principle be correct, if vector could be used with an array.
But you couldn't assign &vec[0] to an array* if the vector
instantiation type was a struct as you suggest.

Dylan



Thu, 20 May 2004 15:18:06 GMT  
 Address of vector::operator[n]() if instantiated with C-array type

Quote:

> > Any reason:

> > struct fiveInt {
> >   int data[5];
> >   int& operator[](int i) {return data[i];}
> > };

> > vector<fiveInt> v;

> > won't work?  fiveInt is Assignmable...You could even make a implicit
> > constructor from a 5 element int array, if you don't control the
structure
> > of your data source.

> Yes because the pointer used to access the data would be of the wrong
> type. Of course you could force it to be the right one, but that
> hardly seems an elegant solution.

> Just to clarify.  What I'm trying to do is:

> typedef int array[5];

> array table[10];
> vector<array> vec;

> const array* data = use_table() ? table : &vec[0];

> Would in principle be correct, if vector could be used with an array.
> But you couldn't assign &vec[0] to an array* if the vector
> instantiation type was a struct as you suggest.

Ah.  That does complicate the problem - what you are asking for is more than
the ability to store arrays in vectors, you actually want the arrays laid
out contiguously in memory.  Fair enough.

I'm not sure if there's a portable way to do what you want.  However, in
practicality I think you'll find the fiveInt solution would have the memory
layout you're seeking on most current compilers.  You could also introduce
an implicit conversion to the array typedef, which would make your example
code compile.

Ken



Fri, 21 May 2004 04:24:59 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Type-Safe array CArray passed as a parameter

2. arrays,vectors,lists with different basic types

3. vector question when replacing array[10][20] with vectors

4. comparision operator ignore type cast operators

5. CArray vs Vector?

6. CArray || vector<>

7. Instantiating manged object w/Type

8. 'operator =' for CArray derived class

9. CString/CArray and Overloaded = Operator

10. operator= and CArray

11. CArray::operator[] (int) const

12. Newbie question, obtain array of instantiated objects?

 

 
Powered by phpBB® Forum Software