Topic: const and typedef


Author: "C dric Naniot" <StarFighter@mail.dotcom.fr>
Date: 2000/01/25
Raw View
Hello,

This is correct C++ code, isn't ?
const unsigned long* const p = 0;

I thought this was correct and equivalent to the above code...

typedef unsigned long* TOTO;
const TOTO const lpsz = 0;

...but MSVC 6 give me this: "warning C4114: same type qualifier used more
than once" and Comeau C/C++ 4.2.42 this: "error: type qualifier specified
more than once
  const TOTO const lpsz = 0;
             ^
"
Why?



[ 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: 2000/01/26
Raw View
"C   dric Naniot" wrote:
>
> Hello,
>
> This is correct C++ code, isn't ?
> const unsigned long* const p = 0;

Yes: p is const, and points to a const unsigned long.

>
> I thought this was correct and equivalent to the above code...
>
> typedef unsigned long* TOTO;
> const TOTO const lpsz = 0;

Not equivalent. A typedef is not a macro, and the const qualifier
can appear before or after the type specifier in a declaration.
The declarations
 const T t; // t is const of type T
 T const t; // t is const of type T
mean exactly the same thing. The three declarations
 TOTO const const lpsz;
 const TOTO const lpsz;
 const const TOTO lpsz;
mean exactly the same thing.  Both of the const qualifiers refer to
lpsz itself, and you are not allowed to repeat the qualifiers.

--
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: Ron Natalie <ron@sensor.com>
Date: 2000/01/26
Raw View

"C   dric Naniot" wrote:
>
> Hello,
>
> This is correct C++ code, isn't ?
> const unsigned long* const p = 0;
p is const, a pointer to a const unsigned long.
>
> I thought this was correct and equivalent to the above code...
>
> typedef unsigned long* TOTO;
TOTO is an type alias meaning "pointer to unsinged long"
> const TOTO const lpsz = 0;

The consts here is as Microsoft tells you redundant.  typedef
isn't a string substition, it defines a type name.  const TOTO const
is entirely analogous to "const int const"  You've got two consts
there that apply to the same thing (C++ allows the const to come
either before or after the typename, but not both).

There is no way to tweak TOTO into meaning pointer to const unsigned
long.

---
[ 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: Nick Ambrose <nick@ixia.com>
Date: 2000/01/26
Raw View

Ron Natalie wrote:

> "C   dric Naniot" wrote:
> >
> > Hello,
> >
> > This is correct C++ code, isn't ?
> > const unsigned long* const p = 0;
> p is const, a pointer to a const unsigned long.
> >
> > I thought this was correct and equivalent to the above code...
> >
> > typedef unsigned long* TOTO;
> TOTO is an type alias meaning "pointer to unsinged long"
> > const TOTO const lpsz = 0;
>
> The consts here is as Microsoft tells you redundant.  typedef
> isn't a string substition, it defines a type name.  const TOTO const
> is entirely analogous to "const int const"  You've got two consts
> there that apply to the same thing (C++ allows the const to come
> either before or after the typename, but not both).

But this is allowed (but still redundant) if it occurs as part of a typedef,
right ?
Nick

--
Nick Ambrose
Senior Software Engineer
Ixia Communications, Inc.
<mailto:nick@ixia.com>
(818) 871-1800 x160

http://www.ixia.com

Ixia...
     When the test really counts.

---
[ 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: comeau@panix.com (Greg Comeau)
Date: 2000/01/26
Raw View
In article <86k1s5$mf7$1@news0.skynet.be> "C   dric Naniot" <StarFighter@mail.dotcom.fr> writes:
>
>Hello,
>
>This is correct C++ code, isn't ?
>const unsigned long* const p = 0;
>
>I thought this was correct and equivalent to the above code...
>
>typedef unsigned long* TOTO;
>const TOTO const lpsz = 0;
>
>...but MSVC 6 give me this: "warning C4114: same type qualifier used more
>than once" and Comeau C/C++ 4.2.42 this: "error: type qualifier specified
>more than once
>  const TOTO const lpsz = 0;
>             ^
>"
>Why?

Because a tyepdef is not a #define, that it, it does not literally
do text substitution.  So even though you know TOTO is
unsigned long *, you just can't plug const's around it as in
your original declaration of p.  What you might want to do is
to just plug in int instead of TOTO:

   const int const lpsz = 0;

Now, it's clearly duplicate.  BTW, we can rewrite the above as:

    const const int lpsz = 0;
    int const const lpsz = 0;

just to hit home that the const is twice.  Using your example then:

    const const TOTO lpsz = 0;
    TOTO const const lpsz = 0;

- Greg
--
Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
Producers of Comeau C/C++ 4.2.42 -- NOTE 4.2.42 NOW AVAILABLE
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.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: 2000/01/26
Raw View
In article <86k1s5$mf7$1@news0.skynet.be>, C   dric Naniot
<StarFighter@mail.dotcom.fr> writes
>Hello,
>
>This is correct C++ code, isn't ?
>const unsigned long* const p = 0;
>
>I thought this was correct and equivalent to the above code...
>
>typedef unsigned long* TOTO;
>const TOTO const lpsz = 0;

by the rules this is equivalent to:

TOTO const const lpsz = 0;

typedefs are not macro substitutions, they provide names for types.

Your problem is why so many of us prefer to stick to providing cv
qualification to the right of the type(name) being qualified.
>
>...but MSVC 6 give me this: "warning C4114: same type qualifier used more
>than once" and Comeau C/C++ 4.2.42 this: "error: type qualifier specified
>more than once
>  const TOTO const lpsz = 0;

Quite correct.

>             ^
>"
>Why?

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: Jonathan Biggar <jon@floorboard.com>
Date: 2000/01/26
Raw View
Steve Clamage wrote:
>
> "C   dric Naniot" wrote:
> >
> > Hello,
> >
> > This is correct C++ code, isn't ?
> > const unsigned long* const p = 0;
>
> Yes: p is const, and points to a const unsigned long.
>
> >
> > I thought this was correct and equivalent to the above code...
> >
> > typedef unsigned long* TOTO;
> > const TOTO const lpsz = 0;
>
> Not equivalent. A typedef is not a macro, and the const qualifier
> can appear before or after the type specifier in a declaration.
> The declarations
>         const T t; // t is const of type T
>         T const t; // t is const of type T
> mean exactly the same thing. The three declarations
>         TOTO const const lpsz;
>         const TOTO const lpsz;
>         const const TOTO lpsz;
> mean exactly the same thing.  Both of the const qualifiers refer to
> lpsz itself, and you are not allowed to repeat the qualifiers.

However, there is a case where this does not apply: const applied to a
typedef of an array propagates into the array to its component type.

typedef char BadWord[4];
typedef const BadWord RealBadWord;

RealBadWord is an "array of 4 const char", not a "const array of 4
char".  The reason for this rule is that the concept of a "const array"
is meaningless unless it is the samething as an array of constant
objects.

--
Jon Biggar
Floorboard Software
jon@floorboard.com
jon@biggar.org


[ 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: Ron Natalie <ron@sensor.com>
Date: 2000/01/27
Raw View

Nick Ambrose wrote:
>
>
> But this is allowed (but still redundant) if it occurs as part of a typedef,
> right ?
> Nick

If you do something like:

 typedef const int cint_t;

 const cint_t x;

Then the compiler will let you slide on the rudundant const.  The
same thing happens with template substitution.

---
[ 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              ]