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


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

Re: Global const objects missing from object file

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c++
Subject Re: Global const objects missing from object file
Date 2021-10-10 12:33 +0200
Organization A noiseless patient Spider
Message-ID <sjufig$ekt$1@dont-email.me> (permalink)
References <sjqap1$sih$1@gioia.aioe.org> <sjrhdp$bta$1@dont-email.me> <sju2pd$1ko$1@gioia.aioe.org>

Show all headers | View raw


On 10/10/2021 08:55, Steve Keller wrote:
> Paavo Helde <myfirstname@osa.pri.ee> writes:
> 
>> Such global const definitions normally appear in a common header file
>> and would just cause duplicate linker symbols if they had external
>> linkage. Now they can be just optimized away in translation units
>> which do not use them.
>>
>> I believe the original motivation was to have an easy replacement of C
>> macros in header files, i.e. instead of
>>
>> #define a 42
>>
>> one can easily use
>>
>> const int a = 42;
>>
>> which would function almost exactly like a #define, plus it honors C++
>> namespaces and cannot be undefined.
> 
> If you write such "global" const objects into a header file they are
> in fact not global, because the implicit 'static' makes them local to
> file translation unit and this can even cause duplication:
> 
>     $ cat foo.hh
>     const int a = 42;
>     
>     const int *bar();
>     $ cat x.cc
>     #include "foo.hh"
>     
>     const int *bar() { return &a; }
>     $ cat y.cc
>     #include <iostream>
>     
>     #include "foo.hh"
>     
>     const int *foo() { return &a; }
>     
>     int main()
>     {
>         std::cout << (void *)foo() << '\n' << (void *)bar() << '\n';
>     }
>     $ g++ -Os -o foo x.cc y.cc
>     $ ./foo
>     0x56027d731008
>     0x56027d731004
> 
> For a small 'int' one may not care but for larger constant objects
> this may be really bad.
> 
> To avoid it, I think you still have to write the defintion into only
> one C++ source file using 'extern'
> 
>     extern const int = 42;

You don't (IMHO) want "extern" here - you want to make sure that this
file includes the header with the "extern const int a;" declaration.
(And you forgot the "a" in that line.)

> 
> and put
> 
>     extern const int a;
> 
> into the header file.  I still don't see the advantage for C++ to
> differ from C here.
> 

It is convenient when you have const objects defined within a file and
that are local to the file - there is no need for a "static".  (A lot of
people are far too lazy about putting "static" where it is appropriate
in their C and C++ coding.)

With C++17, you can also write:

	extern inline const int a = 42;

in the header, and you don't need a definition in a C++ file.

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


Thread

Global const objects missing from object file Steve Keller <keller.steve@gmx.de> - 2021-10-08 22:47 +0200
  Re: Global const objects missing from object file red floyd <no.spam.here@its.invalid> - 2021-10-08 15:09 -0700
    Re: Global const objects missing from object file Steve Keller <keller.steve@gmx.de> - 2021-10-10 08:28 +0200
      Re: Global const objects missing from object file Paavo Helde <myfirstname@osa.pri.ee> - 2021-10-10 12:03 +0300
      Re: Global const objects missing from object file David Brown <david.brown@hesbynett.no> - 2021-10-10 12:29 +0200
  Re: Global const objects missing from object file Paavo Helde <myfirstname@osa.pri.ee> - 2021-10-09 10:47 +0300
    Re: Global const objects missing from object file scott@slp53.sl.home (Scott Lurndal) - 2021-10-09 14:32 +0000
      Re: Global const objects missing from object file Paavo Helde <myfirstname@osa.pri.ee> - 2021-10-09 21:39 +0300
    Re: Global const objects missing from object file Steve Keller <keller.steve@gmx.de> - 2021-10-10 08:55 +0200
      Re: Global const objects missing from object file Bo Persson <bo@bo-persson.se> - 2021-10-10 10:34 +0200
        Re: Global const objects missing from object file Steve Keller <keller.steve@gmx.de> - 2021-10-10 15:55 +0200
          Re: Global const objects missing from object file Paavo Helde <myfirstname@osa.pri.ee> - 2021-10-10 17:28 +0300
            Re: Global const objects missing from object file Steve Keller <keller.steve@gmx.de> - 2021-10-10 20:42 +0200
              Re: Global const objects missing from object file Paavo Helde <myfirstname@osa.pri.ee> - 2021-10-10 22:44 +0300
      Re: Global const objects missing from object file Paavo Helde <myfirstname@osa.pri.ee> - 2021-10-10 12:14 +0300
        Re: Global const objects missing from object file Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-10-29 06:29 -0700
          Re: Global const objects missing from object file Bo Persson <bo@bo-persson.se> - 2021-10-29 15:55 +0200
            Re: Global const objects missing from object file Öö Tiib <ootiib@hot.ee> - 2021-10-29 22:20 -0700
            Re: Global const objects missing from object file Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-11-07 04:51 -0800
          Re: Global const objects missing from object file scott@slp53.sl.home (Scott Lurndal) - 2021-10-29 14:44 +0000
            Re: Global const objects missing from object file Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-11-07 05:08 -0800
              Re: Global const objects missing from object file scott@slp53.sl.home (Scott Lurndal) - 2021-11-07 14:21 +0000
      Re: Global const objects missing from object file David Brown <david.brown@hesbynett.no> - 2021-10-10 12:33 +0200
  Re: Global const objects missing from object file "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2021-10-09 15:05 +0200
  Re: Global const objects missing from object file Juha Nieminen <nospam@thanks.invalid> - 2021-10-09 17:40 +0000

csiph-web