Topic: Converting from a array to a const array reference - Valid ?


Author: Morten Christensen <mor@dit.ou.dk>
Date: 1996/06/26
Raw View
Hi, can anyone please tell me if the small piece of code
below is valid in standard C++ ? One compiler
(Borland 5.0) accepts it, another compiler (MSVC++ 4.0)
don't (complains that it can't convert the type).

In other words, is this a feature or a BUG in MSVC++ ?

C++ Example:

  typedef int T[3];

  int main()
  {
   T a;
   T& ra = a; // This compiles ok.
   const T& cra = a; // ??? Won't compile using MSVC++!

   return 0;
  }


Thanks for your help,
Morten M. Christensen
Odense Lindoe, Denmark


[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: paul.black@vf.vodafone.co.uk
Date: 1996/06/27
Raw View
Morten Christensen <mor@dit.ou.dk> wrote:
> Hi, can anyone please tell me if the small piece of code
> below is valid in standard C++ ? One compiler
> (Borland 5.0) accepts it, another compiler (MSVC++ 4.0)
> don't (complains that it can't convert the type).
>
> In other words, is this a feature or a BUG in MSVC++ ?
>
> C++ Example:
>
>   typedef int T[3];
>
>   int main()
>   {
>    T a;
>    T& ra = a; // This compiles ok.
>    const T& cra = a; // ??? Won't compile using MSVC++!
>
>    return 0;
>   }
>
>
> Thanks for your help,
> Morten M. Christensen
> Odense Lindoe, Denmark
>

I think this is a bug in MSVC++. Section 8.5.3 (References) of the draft
standard allows the reference to be more cv-qualified than the object that it
refers to. The example in the text is:
    double d = 2.0;
    double& rd = d;
    const double& rcd = d;

Since a typedef is just a synonym for another type, the above code should
compile.

Paul
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: b91926@fsgi02.fnal.gov (David Sachs)
Date: 1996/06/28
Raw View
The trouble is that array types are "inferior" to other types
in some aspects. If you write:

typedef int T[3];
const T x;

you are NOT creating an array of 3 constant integers. Instead you
are specifying a "constant array" of 3 (non-constant) integers. As
far as I know the C++ language does not provide semantics for
constant or volatile arrays.

Another way in which arrays are "inferior" to other types is that
you cannot write:

T* x = new T;
--
** The Klingons' favorite food was named by the first earthling to see it **
David Sachs - Fermilab, MSSG MS369 - P. O. Box 500 - Batavia, IL 60510
Voice: 1 708 840 3942      Deparment Fax: 1 708 840 3785
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: JamesC@MonsterSystems.com (James J. Couball)
Date: 1996/06/28
Raw View
Morten Christensen <mor@dit.ou.dk> wrote:

>Hi, can anyone please tell me if the small piece of code
>below is valid in standard C++ ? One compiler
>(Borland 5.0) accepts it, another compiler (MSVC++ 4.0)
>don't (complains that it can't convert the type).
>
>In other words, is this a feature or a BUG in MSVC++ ?
>
>C++ Example:
>
>  typedef int T[3];
>
>  int main()
>  {
>   T a;
>   T& ra = a; // This compiles ok.
>   const T& cra = a; // ??? Won't compile using MSVC++!
>
>   return 0;
>  }
>

Morten,

I tried this code in MSVC++ 4.1 and it compiled
with no errors.

James Couball.
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: seurer@rchland.ibm.com (Bill Seurer)
Date: 1996/06/28
Raw View
In article <31D14C17.4FA3@dit.ou.dk>, Morten Christensen <mor@dit.ou.dk> writes
:
|> Hi, can anyone please tell me if the small piece of code
|> below is valid in standard C++ ? One compiler
|> (Borland 5.0) accepts it, another compiler (MSVC++ 4.0)
|> don't (complains that it can't convert the type).
|>
|> In other words, is this a feature or a BUG in MSVC++ ?
|>
|> C++ Example:
|>
|>   typedef int T[3];
|>
|>   int main()
|>   {
|>    T a;
|>    T& ra = a; // This compiles ok.
|>    const T& cra = a; // ??? Won't compile using MSVC++!
|>
|>    return 0;
|>   }

It looks like a bug.  Note though that this isn't "converting" anything
but is setting up a const reference to a non-const variable which is fine.
The other way around won't work of course.
--

- Bill Seurer     ID Tools and Compiler Development      IBM Rochester, MN
  Business: BillSeurer@vnet.ibm.com               Home: BillSeurer@aol.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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/06/28
Raw View
b91926@fsgi02.fnal.gov (David Sachs) writes:

>The trouble is that array types are "inferior" to other types
>in some aspects.

True, but I'm not sure it's revelant to this example.

>If you write:
>
>typedef int T[3];
>const T x;
>
>you are NOT creating an array of 3 constant integers.

That's wrong.  See 3.9.3 [basic.type.qualifier], paragraph 2:

|   3.9.3  CV-qualifiers                            [basic.type.qualifier]
|
| 2 A  compound  type  (_basic.compound_)  is  not cv-qualified by the cv-
|   qualifiers (if any) of the type from which it is compounded.  Any  cv-
|   qualifiers that appear in an array declaration apply to the array ele-
|   ment type, not the array type (_dcl.array_).

This means that a declaration like the above DOES create an array of
3 constant integers.

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
Date: 1996/06/28
Raw View
In article <4qvahn$5b7@fsgi02.fnal.gov> b91926@fsgi02.fnal.gov (David
Sachs) writes:

|> The trouble is that array types are "inferior" to other types
|> in some aspects. If you write:

|> typedef int T[3];
|> const T x;

|> you are NOT creating an array of 3 constant integers. Instead you
|> are specifying a "constant array" of 3 (non-constant) integers. As
|> far as I know the C++ language does not provide semantics for
|> constant or volatile arrays.

Not quite.  I'm not that familiar with the wording of the C++ draft
standard, but at least in C, there is no such thing as a const array;
declaring an array with the keyword const means that each member of the
array is const.  (I think that this is just a minor problem with the
wording, and that the intent is for the array to also be const.  But
the individual elements are definitly const.)
--
James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils,    tudes et r   alisations en logiciel orient    objet --
                -- A la recherche d'une activit    dans une region francophone
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]