Topic: Const array as class data member


Author: David R Tribble <david@tribble.com>
Date: 1999/12/22
Raw View
Steve Clamage wrote:
>
> Justin wrote:
>> Joseph Hesse <joe_hesse@uswest.net> wrote in message
>> news:iG924.68$dd.15481@news.uswest.net...
>> >
>> > I can't find anything in the standard allowing or disallowing a
>> > const array as a class data member.  ...  If they are legal, how
>> > does one initialize them?
>>
>>
>> I guess that those const array elements could be initialized in
>> the member initialization list with the constructor.
>
> Not a very good guess. There is no syntax that allows you to
> initialize a C-style array in a member init list. That is, you
> have an identifier followed by an expression-list in parentheses,
> but you cannot initialize an array that way.

Has anyone proposed adding something to the [next revision of the]
language to allow explicit member initialization of arrays?  This
seems like an important feature that was unintentionally left out.

I could see a few possible syntactical constructs for this:

    class Foo
    {
        int    arr[10];    // could also be const
    };

    // A
    Foo::Foo():
        arr()          // default-initializes arr[*]
    { ... }

    // B
    Foo::Foo():
        arr[0](),      // default-initializes arr[0]
        arr[1](2)      // initializes arr[1] to 2
    { ... }

    // C
    Foo::Foo():
        arr( { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } )
                       // initializes arr[0]...arr[9]
    { ... }

    // D
    Foo::Foo():
        arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
                       // initializes arr[0]...arr[9]
    { ... }

-- David R. Tribble, david@tribble.com, http://david.tribble.com --
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Justin" <suawen@ms3.url.com.tw>
Date: 1999/12/09
Raw View
Hi,

I guess that those const array elements could be initialized in the member
initialization list with the constructor.

Joseph Hesse <joe_hesse@uswest.net> wrote in message
news:iG924.68$dd.15481@news.uswest.net...
>
> Hi,
> I can't find anything in the standard allowing or disallowing a const
array
> as a class data member.  Visual C++ 6 and Borland C++ Builder 4 allow
const
> arrays.  If they are legal, how does on initialize them?  There doesn't
> appear to be any initializer list syntax to initialize a const array.  The
> following code works but seems real ugly to me.
> Thanks for reading this.
> Joe Hesse
>
> ---------- code starts here ----------
> class X
> {
> private:
>   const int ciarr[2];
> public:
>   X(int a, int b);
> };
>
> X::X(int a, int b)
> {
>   // ugly code to initialize const array
>   int *p = const_cast<int *>(ciarr);
>   p[0] = a;
>   p[1] = b;
> }
>
> ---------- code ends here ----------
>
>
>
>
>
>
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
>
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Steve Clamage <stephen.clamage@sun.com>
Date: 1999/12/11
Raw View
Justin wrote:
> Joseph Hesse <joe_hesse@uswest.net> wrote in message
> news:iG924.68$dd.15481@news.uswest.net...
> >
> > I can't find anything in the standard allowing or disallowing a const array
> > as a class data member.  Visual C++ 6 and Borland C++ Builder 4 allow const
> > arrays.  If they are legal, how does on initialize them?
>
>
> I guess that those const array elements could be initialized in the member
> initialization list with the constructor.

Not a very good guess. There is no syntax that allows you to
initialize a C-style array in a member init list. That is, you
have an identifier followed by an expression-list in parentheses,
but you cannot initialize an array that way.

--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/12/11
Raw View
In article <82ojo4$gh0$3@news.seed.net.tw>, Justin
<suawen@ms3.url.com.tw> writes
>I guess that those const array elements could be initialized in the member
>initialization list with the constructor.

Except that there is no syntax available to do so.


Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Eric Nagler <epn@eric-nagler.com>
Date: 1999/12/08
Raw View
On 7 Dec 1999 18:01:31 GMT, "Joseph Hesse" <joe_hesse@uswest.net>
wrote:

>
>Eric,
>I just compiled the code I originally posted with the command:
>c:\> bcc32 /c test.cpp
>The code compiled with only a single warning.  The output of the compiler
>was:
>
>Borland C++ 5.4 for Win32 Copyright (c) 1993, 1999 Inprise Corporation
>test.cpp:
>Warning W8038 test.cpp 9: Constant member 'X::ciarr' is not initialized in
>function X::X(int,int)

Amazing. This is what I get:
Borland C++ 5.4 for Win32 Copyright (c) 1993, 1999 Inprise Corporation
c:\data\TESTING\test.cpp:
Error E2232 c:\data\TESTING\test.cpp 5: Constant member 'X::array' in
class without constructors
*** 1 errors in Compile ***

Are you running with the latest patches? Where does the name 'ciarr'
come from?

[ moderator's note: This thread is drifting away from any relevance
  to the C++ standard. Discussions about which versions of which
  compilers have particular bugs ordinarily is off-topic. Please
  keep any replies on-topic.  -sdc ]


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: David R Tribble <david@tribble.com>
Date: 1999/12/08
Raw View
Joseph Hesse wrote:
>> I can't find anything in the standard allowing or disallowing a
>> const array as a class data member.  Visual C++ 6 and Borland C++
>> Builder 4 allow const arrays.  If they are legal, how does on
>> initialize them?  There doesn't appear to be any initializer list
>> syntax to initialize a const array.  The following code works but
>> seems real ugly to me.
>>
>> ---------- code starts here ----------
>>  class X
>>  {
>>  private:
>>    const int ciarr[2];
>>  public:
>>    X(int a, int b);
>>  };
>>
>>  X::X(int a, int b)
>>  {
>>    // ugly code to initialize const array
>>    int *p = const_cast<int *>(ciarr);
>>    p[0] = a;
>>    p[1] = b;
>>  }

James Kuyper Jr. wrote:
> Section 12, p1: "const and volatile semantics (7.1.5.1) are not
> applied on an object under construction. Such semantics only come
> into effect once the constructor for the most derived object (1.8)
> ends."

That's correct, but it doesn't solve the problem, because once the
body of X::X() begins to execute, all of the member variables of
'this' have been constructed, including the const array 'ciarr'.

The solution is to use an explicit member initializer list in the
ctor (see 12.6.2):

    X::X(int a, int b):
        ciarr()       // explicit default initializer for ciarr[]
    {
        // at this point, ciarr[] is default-initialized
        ...
    }

(I believe this is legal, though I'm not positively sure.)

If you want to initialize a const member variable with something
other than its default value (which is usually all zeros), you can
violate const semantics by using a non-const pointer, such as
what is done in the quoted code above.

The ctor is somewhat justified in doing this kind of thing, since
class X is the owner of X::ciarr[], but violating const safety is
a questionable practice.  There are probably more kosher ways of
doing this, but I can't think of any.

-- David R. Tribble, david@tribble.com, http://david.tribble.com --


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Eric Nagler <epn@eric-nagler.com>
Date: 1999/12/06
Raw View
On 4 Dec 1999 23:52:20 GMT, "Joseph Hesse" <joe_hesse@uswest.net>
wrote:

>
>Hi,
>I can't find anything in the standard allowing or disallowing a const array
>as a class data member.  Visual C++ 6 and Borland C++ Builder 4 allow const
>arrays.  If they are legal, how does on initialize them?  There doesn't
>appear to be any initializer list syntax to initialize a const array.  The
>following code works but seems real ugly to me.
>Thanks for reading this.

IMHO, it's a hole in the language. A const array inside a class
definition really is an oxymoron because a 'const' MUST be
initialized, and yet an array CANNOT be initialized (in the true sense
of initialization). CBuilder 3 and 4 both reject such a declaration.

class X
{
  const int array[1];  // Error
};

So I don't know how you can state that it's really allowed.

EriC++



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Biju Thomas <b_thomas@ibm.net>
Date: 1999/12/06
Raw View
Joseph Hesse wrote:
>
> I can't find anything in the standard allowing or disallowing a const array
> as a class data member.  Visual C++ 6 and Borland C++ Builder 4 allow const
> arrays.  If they are legal, how does on initialize them?

For POD classes, you can use the brace-initialization-list to initialize
the array. For example:

  class X {
  public:
    const int ciarr[2];
  };

  X aX = { 1, 2 };

--
Regards,
Biju Thomas


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Joseph Hesse" <joe_hesse@uswest.net>
Date: 1999/12/06
Raw View
Eric,

What if it is a const array of another class with a default constructor,
e.g.,

class A {};  // assume A has a default constructor
class X {
  const A array[2];
 // ...
};

In class X, the const array is initialized without any specific initializer
syntax since class A has a default constructor.  So, is the rule : a const
array in a class is legal iff it is a const array of a class with a default
constructor?  I don't know the answer and I can't find anything in the
standard that addresses this question.

By the way, the code I posted compiles with a warning with Borland C++
Builder 4.

Thanks,
Joe

"Eric Nagler" <epn@eric-nagler.com> wrote in message
news:dm5KOHXINAqNTW=a=BPVHljGuCBa@4ax.com...
>
> On 4 Dec 1999 23:52:20 GMT, "Joseph Hesse" <joe_hesse@uswest.net>
> wrote:
>
> >
> >Hi,
> >I can't find anything in the standard allowing or disallowing a const array
> >as a class data member.  Visual C++ 6 and Borland C++ Builder 4 allow const
> >arrays.  If they are legal, how does on initialize them?  There doesn't
> >appear to be any initializer list syntax to initialize a const array.  The
> >following code works but seems real ugly to me.
> >Thanks for reading this.
>
> IMHO, it's a hole in the language. A const array inside a class
> definition really is an oxymoron because a 'const' MUST be
> initialized, and yet an array CANNOT be initialized (in the true sense
> of initialization). CBuilder 3 and 4 both reject such a declaration.
>
> class X
> {
>   const int array[1];  // Error
> };
>
> So I don't know how you can state that it's really allowed.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/12/07
Raw View
Joseph Hesse wrote:
>
> Hi,
> I can't find anything in the standard allowing or disallowing a const array
> as a class data member.  Visual C++ 6 and Borland C++ Builder 4 allow const
> arrays.  If they are legal, how does on initialize them?  There doesn't
> appear to be any initializer list syntax to initialize a const array.  The
> following code works but seems real ugly to me.
> Thanks for reading this.
> Joe Hesse
>
> ---------- code starts here ----------
> class X
> {
> private:
>   const int ciarr[2];
> public:
>   X(int a, int b);
> };
>
> X::X(int a, int b)
> {
>   // ugly code to initialize const array
>   int *p = const_cast<int *>(ciarr);
>   p[0] = a;
>   p[1] = b;
> }

Section 12, p1: "const and volatile semantics (7.1.5.1) are not applied
on an object under construction. Such semantics only come into effect
once the constructor for the most derived object (1.8) ends."
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Eric Nagler <epn@eric-nagler.com>
Date: 1999/12/07
Raw View
On 6 Dec 1999 16:24:27 GMT, "Joseph Hesse" <joe_hesse@uswest.net>
wrote:

>
>Eric,
>
>What if it is a const array of another class with a default constructor,
>e.g.,
>
>class A {};  // assume A has a default constructor
>class X {
>  const A array[2];
> // ...
>};
>
>In class X, the const array is initialized without any specific initializer
>syntax since class A has a default constructor.  So, is the rule : a const
>array in a class is legal iff it is a const array of a class with a default
>constructor?  I don't know the answer and I can't find anything in the
>standard that addresses this question.
>
>By the way, the code I posted compiles with a warning with Borland C++
>Builder 4.

Really? I get a fatal error:
Error E2232 c:\data\TESTING\test.cpp 5: Constant member 'X::array' in
class without constructors
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Joseph Hesse" <joe_hesse@uswest.net>
Date: 1999/12/07
Raw View
Eric,
I just compiled the code I originally posted with the command:
c:\> bcc32 /c test.cpp
The code compiled with only a single warning.  The output of the compiler
was:

Borland C++ 5.4 for Win32 Copyright (c) 1993, 1999 Inprise Corporation
test.cpp:
Warning W8038 test.cpp 9: Constant member 'X::ciarr' is not initialized in
function X::X(int,int)

Joe


"Eric Nagler" <epn@eric-nagler.com> wrote in message
news:8O1LOELiUB56SezK1YZSqL7cgRq1@4ax.com...
> On 6 Dec 1999 16:24:27 GMT, "Joseph Hesse" <joe_hesse@uswest.net>
> wrote:
>
> >
> >Eric,
> >
> >What if it is a const array of another class with a default constructor,
> >e.g.,
> >
> >class A {};  // assume A has a default constructor
> >class X {
> >  const A array[2];
> > // ...
> >};
> >
> >In class X, the const array is initialized without any specific initializer
> >syntax since class A has a default constructor.  So, is the rule : a const
> >array in a class is legal iff it is a const array of a class with a default
> >constructor?  I don't know the answer and I can't find anything in the
> >standard that addresses this question.
> >
> >By the way, the code I posted compiles with a warning with Borland C++
> >Builder 4.
>
> Really? I get a fatal error:
> Error E2232 c:\data\TESTING\test.cpp 5: Constant member 'X::array' in
> class without constructors


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Joseph Hesse" <joe_hesse@uswest.net>
Date: 1999/12/04
Raw View
Hi,
I can't find anything in the standard allowing or disallowing a const array
as a class data member.  Visual C++ 6 and Borland C++ Builder 4 allow const
arrays.  If they are legal, how does on initialize them?  There doesn't
appear to be any initializer list syntax to initialize a const array.  The
following code works but seems real ugly to me.
Thanks for reading this.
Joe Hesse

---------- code starts here ----------
class X
{
private:
  const int ciarr[2];
public:
  X(int a, int b);
};

X::X(int a, int b)
{
  // ugly code to initialize const array
  int *p = const_cast<int *>(ciarr);
  p[0] = a;
  p[1] = b;
}

---------- code ends here ----------






[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]