Topic: extern const


Author: roland.muller@nokia.com (=?ISO-8859-1?Q?Roland_M=FCller?=)
Date: Thu, 28 Nov 2002 20:04:43 +0000 (UTC)
Raw View
Hello,

is the following code legal according to the C++ standard? It compiles=20
with gcc 2.95.x and also gcc 3.2 but not with other compilers.

    * extern const short LENGTH_MAX_C;

      int foo()
      {
              char tmp[LENGTH_MAX_C];
      }
    * the value of LENGTH_MAX_C is defined in anther .cc file

Thank you in advance!
-Roland M=FCller

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: v.Abazarov@attAbi.com ("Victor Bazarov")
Date: Fri, 29 Nov 2002 14:57:57 +0000 (UTC)
Raw View
"Roland M   ller" <roland.muller@nokia.com> wrote...
> is the following code legal according to the C++ standard? It compiles
> with gcc 2.95.x and also gcc 3.2 but not with other compilers.
>
>     * extern const short LENGTH_MAX_C;
>
>       int foo()
>       {
>               char tmp[LENGTH_MAX_C];
>       }
>     * the value of LENGTH_MAX_C is defined in anther .cc file


No, the code is illegal.  An array size has to be compile-time
constant (meaning that its value has to be known at the time
the array is defined).  GCC has run-time "dynamic" arrays as
a language extension.

Victor
--
Please remove capital A's from my address when replying by mail

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kanze@gabi-soft.de (James Kanze)
Date: Fri, 29 Nov 2002 14:58:10 +0000 (UTC)
Raw View
roland.muller@nokia.com (?ISO-8859-1?Q?Roland M ller?) wrote in message
news:<3DE6204A.909@nokia.com>...

> is the following code legal according to the C++ standard? It compiles
> with gcc 2.95.x and also gcc 3.2 but not with other compilers.

>     * extern const short LENGTH MAX C;

>       int foo()
>       {
>               char tmp[LENGTH MAX C];
>       }
>     * the value of LENGTH MAX C is defined in anther .cc file

According to the standard, it depends on how the value of LENGTH_MAX_C
is defined in the other .cc file; if it is initialized with an integral
constant expression, the code is legal.

In fact, this is doubtlessly an error in the standard.  If separate
compilation is used, the compiler cannot know how it is initialized in
the other .cc.

If g++ accepts it, it is only because you forgot to give it the options
telling it that it was to be a C++ compiler (-ansi -pedantic).  G++
accepts it as an extention; g++ would even accept it if LENGTH_MAX_C
were not const.

It IS legal C (even if LENGTH_MAX_C is not const).  For this reason, I
would not be surprised if more compilers started to accept it.

--
James Kanze                           mailto:jkanze@caicheuvreux.com
Conseils en informatique orient   e objet/
                    Beratung in objektorientierter Datenverarbeitung

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]