Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #8938

Re: Array inside struct inside container

From Victor Bazarov <v.bazarov@comcast.invalid>
Newsgroups comp.lang.c++
Subject Re: Array inside struct inside container
Date 2011-08-16 15:28 -0400
Organization A noiseless patient Spider
Message-ID <j2egda$5cc$1@dont-email.me> (permalink)
References <Xns9F43865A8800Dnonenonenone@69.16.186.52>

Show all headers | View raw


On 8/16/2011 3:12 PM, none wrote:
> I've seen forum posts here and there asking "How do I create an STL
> container of arrays?"  Or, equivalently, "How do I fix this syntax:"
>
>     std::vector<float[20]>  v;
>
> The answers to these questions are generally one of two things:
>
> 1) Use a boost::array, or
> 2) Put the array in a struct, like so:
>
>     // In header file
>     stuct wrapper { float f[20]; };
>
>     // In CPP file
>     std::vector<wrapper>  v;
>
> I was under the impression that things stored in STL containers needed to
> be copyable and assignable.  That second option seems incorrect to me,
> unless the compiler is expected to generate both an operator= and a copy
> constructor for the struct that perform a memcpy() for the contents of f.

Not memcpy.  See below.

> Is it?

A struct with an array as a data member is assignable.  The default 
compiler-provided assignment operator performs memberwise assignment 
that for array members means element-wise assignment.  You can verify it 
by compiling/running this test program:

    #include <iostream>

    struct A {
      A& operator=(A const&) { std::cout << "A::op=\n"; return *this; }
    };
    struct B { A aa[10]; };

    int main() {
        B b1, b2;
        b1 = b2;
    }

V
-- 
I do not respond to top-posted replies, please don't ask

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Array inside struct inside container none <none@none.none> - 2011-08-16 19:12 +0000
  Re: Array inside struct inside container Victor Bazarov <v.bazarov@comcast.invalid> - 2011-08-16 15:28 -0400
    Re: Array inside struct inside container none <none@none.none> - 2011-08-16 20:07 +0000
      Re: Array inside struct inside container Leigh Johnston <leigh@i42.co.uk> - 2011-08-16 21:18 +0100
        Re: Array inside struct inside container James Kanze <james.kanze@gmail.com> - 2011-08-22 11:01 -0700
          Re: Array inside struct inside container Leigh Johnston <leigh@i42.co.uk> - 2011-08-22 20:44 +0100
            Re: Array inside struct inside container Leigh Johnston <leigh@i42.co.uk> - 2011-08-22 20:51 +0100
      Re: Array inside struct inside container Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-08-16 23:26 +0200
      Re: Array inside struct inside container "Bo Persson" <bop@gmb.dk> - 2011-08-17 12:29 +0200
        Re: Array inside struct inside container Juha Nieminen <nospam@thanks.invalid> - 2011-08-17 13:06 +0000
  Re: Array inside struct inside container yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2011-08-22 13:20 +0000
    Re: Array inside struct inside container Leigh Johnston <leigh@i42.co.uk> - 2011-08-22 14:42 +0100
      Re: Array inside struct inside container yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2011-08-23 09:20 +0000

csiph-web