MESSAGE
DATE | 2016-11-21 |
FROM | ruben safir
|
SUBJECT | Subject: [Learn] Fwd: hidden static
|
From learn-bounces-at-nylxs.com Mon Nov 21 15:52:12 2016 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: from www.mrbrklyn.com (www.mrbrklyn.com [96.57.23.82]) by mrbrklyn.com (Postfix) with ESMTP id D276A161314; Mon, 21 Nov 2016 15:52:11 -0500 (EST) X-Original-To: learn-at-nylxs.com Delivered-To: learn-at-nylxs.com Received: from [10.0.0.62] (flatbush.mrbrklyn.com [10.0.0.62]) by mrbrklyn.com (Postfix) with ESMTP id 66268161312 for ; Mon, 21 Nov 2016 15:52:09 -0500 (EST) References: <20161121144815.63a2899c-at-dell.homenet> <20161121173103.17c0ed02-at-dell.homenet> <20161121181806.58043cf8-at-dell.homenet> To: learn-at-nylxs.com From: ruben safir X-Forwarded-Message-Id: <20161121144815.63a2899c-at-dell.homenet> <20161121173103.17c0ed02-at-dell.homenet> <20161121181806.58043cf8-at-dell.homenet> Message-ID: <98fa3748-b6c8-999c-5105-fc72190b6375-at-mrbrklyn.com> Date: Mon, 21 Nov 2016 15:52:09 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <20161121181806.58043cf8-at-dell.homenet> Content-Type: multipart/mixed; boundary="------------FFF26FD058A6765DB3A66A85" Subject: [Learn] Fwd: hidden static X-BeenThere: learn-at-nylxs.com X-Mailman-Version: 2.1.17 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: learn-bounces-at-nylxs.com Sender: "Learn"
This is a multi-part message in MIME format. --------------FFF26FD058A6765DB3A66A85 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit
C++ static variables --
--------------FFF26FD058A6765DB3A66A85 Content-Type: message/rfc822; name="hidden static.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="hidden static.eml"
Path: reader2.panix.com!panix!not-for-mail From: Popping mad Newsgroups: comp.lang.c++ Subject: hidden static Date: Sat, 19 Nov 2016 01:49:12 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Message-ID: NNTP-Posting-Host: www.mrbrklyn.com Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reader2.panix.com 1479520152 17183 96.57.23.82 (19 Nov 2016 01:49:12 GMT) X-Complaints-To: abuse-at-panix.com NNTP-Posting-Date: Sat, 19 Nov 2016 01:49:12 +0000 (UTC) User-Agent: Pan/0.140 (Chocolate Salty Balls; GIT b8fc14e git.gnome.org/git/pan2) Xref: panix comp.lang.c++:1125334
I've a declared static as a public member of an object but it doesn't seem to show up in the compiler
/home/ruben/src/test_static/test.cpp|22| undefined reference to `blah::A::here'
If I put it on top it errors are a private member. When I move it to public, it is not being seen
The three test case files look like this
test.cpp
18 #include "test.h" 19 #include 20 namespace blah{ 21 void A::read(int in){ 22 A::here = in; 23 std::cout << here << std::endl; 24 } 25 }
test.h
1 namespace blah{ 2 class A{ 3 public: 4 static int here; 5 A(int in=0){ 6 here = in; 7 }; 8 void read(int); 9 }; 10 11 } ~
main.cpp 18 #include "test.h" 19 #include 20 namespace std{ 21 22 int main(int argc, char** argv){ 23 24 blah::A* a = new blah::A{9}; 25 cout << a->here << endl; 26 return 0; 27 } 28 29 } ~ makefile
1 CXX:=g++ 2 CXXFLAGS:=-Wall -ggdb -pg -pthread 3 4 LDFLAGS:=-L/usr/local/lib/mysql -lmysqlpp -lmysqlclient 5 6 test : test.o main.o 7 ${CXX} ${CXXFLAGS} -o testme test.o main.o 8 9 main.o : main.cpp 10 ${CXX} ${CXXFLAGS} -o main.o -c main.cpp 11 12 test.o : test.cpp test.h 13 ${CXX} ${CXXFLAGS} -c test.cpp 14 15 clean : 16 rm testme *.o make.deps 17 touch *.cpp *.h 18 19 include make.deps 20 make.deps: *.cpp ; gcc -M *.cpp >$-at- ~
I need an extern? 1 || g++ -Wall -ggdb -pg -pthread -o testme test.o main.o 2 || /usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../lib/gcrt1.o: In function `_start': 3 || (.text+0x20): undefined reference to `main' 4 || test.o: In function `blah::A::read(int)': 5 /home/ruben/src/test_static/test.cpp|22| undefined reference to `blah::A::here' 6 /home/ruben/src/test_static/test.cpp|23| undefined reference to `blah::A::here' 7 || main.o: In function `std::main(int, char**)': 8 /home/ruben/src/test_static/main.cpp|25| undefined reference to `blah::A::here' 9 || main.o: In function `blah::A::A(int)': 10 /home/ruben/src/test_static/test.h|6| undefined reference to `blah::A::here'
--------------FFF26FD058A6765DB3A66A85 Content-Type: message/rfc822; name="Re: hidden static.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: hidden static.eml"
Path: reader2.panix.com!panix!goblin2!goblin1!goblin.stu.neva.ru!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Ian Collins Newsgroups: comp.lang.c++ Subject: Re: hidden static Date: Sat, 19 Nov 2016 16:07:01 +1300 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net +iTYoWlqtAMDes8BxMG/tgguX4gbR1m7ps3eeKYPyQQeA2t6ol Cancel-Lock: sha1:zkBTSQKoPtOuqGD2BtJcB3Qke60= User-Agent: Mozilla/5.0 (X11; SunOS i86pc; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 In-Reply-To: Xref: panix comp.lang.c++:1125336
On 11/19/16 02:49 PM, Popping mad wrote: > I've a declared static as a public member of an > object but it doesn't seem to show up in the compiler > > /home/ruben/src/test_static/test.cpp|22| undefined reference to `blah::A::here' > > > If I put it on top it errors are a private member. When I move it > to public, it is not being seen > > The three test case files look like this > > > test.cpp > > 18 #include "test.h" > 19 #include > 20 namespace blah{ > 21 void A::read(int in){ > 22 A::here = in; > 23 std::cout << here << std::endl; > 24 } > 25 } > > > test.h > > 1 namespace blah{ > 2 class A{ > 3 public: > 4 static int here; > 5 A(int in=0){ > 6 here = in; > 7 }; > 8 void read(int); > 9 }; > 10 > 11 }
Who have declared A::here, but you haven't defined it anywhere.
-- Ian
--------------FFF26FD058A6765DB3A66A85 Content-Type: message/rfc822; name="Re: hidden static.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: hidden static.eml"
Path: reader2.panix.com!panix!goblin3!goblin2!goblin1!goblin.stu.neva.ru!border1.nntp.ams1.giganews.com!nntp.giganews.com!buffer1.nntp.ams1.giganews.com!buffer2.nntp.ams1.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Sat, 19 Nov 2016 02:30:11 -0600 Date: Sat, 19 Nov 2016 10:30:14 +0200 From: Paavo Helde User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.8) Gecko/20151117 FossaMail/25.1.9 MIME-Version: 1.0 Newsgroups: comp.lang.c++ Subject: Re: hidden static References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID: X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-903oDxr+2ADw8BBbVs2Wz3ceC59ib5ESQIqatdjeszyty77md085+f0YNVQKAaL7m1tFJCAWWD6TXLW!u5UoeRYBUd4cgymY33AvbxrQMTVlSeIibymaV9cO7oX2EamuJ89UeI7sVRHPM+codf4cZZFYFms= X-Complaints-To: abuse-at-giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 1503 Xref: panix comp.lang.c++:1125337
On 19.11.2016 3:49, Popping mad wrote: > I've a declared static as a public member of an > object but it doesn't seem to show up in the compiler > > /home/ruben/src/test_static/test.cpp|22| undefined reference to `blah::A::here'
See https://isocpp.org/wiki/faq/ctors#link-errs-static-data-mems
While there, consider reading the whole https://isocpp.org/faq
--------------FFF26FD058A6765DB3A66A85 Content-Type: message/rfc822; name="Re: hidden static.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: hidden static.eml"
Path: reader2.panix.com!panix!not-for-mail From: Popping mad Newsgroups: comp.lang.c++ Subject: Re: hidden static Date: Sat, 19 Nov 2016 17:01:03 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Message-ID: References: NNTP-Posting-Host: www.mrbrklyn.com Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reader2.panix.com 1479574863 17183 96.57.23.82 (19 Nov 2016 17:01:03 GMT) X-Complaints-To: abuse-at-panix.com NNTP-Posting-Date: Sat, 19 Nov 2016 17:01:03 +0000 (UTC) User-Agent: Pan/0.140 (Chocolate Salty Balls; GIT b8fc14e git.gnome.org/git/pan2) Xref: panix comp.lang.c++:1125342
On Sat, 19 Nov 2016 10:30:14 +0200, Paavo Helde wrote:
> See https://isocpp.org/wiki/faq/ctors#link-errs-static-data-mems > > While there, consider reading the whole https://isocpp.org/faq
This is an excellent explanation, but I'm still not clear on this. I think it is saying that a static has to be declared and defined within a compilational unit. I'm not sure why, though. And I've asked before, and read, but I'm still not clear on what a compilation unit is.
--------------FFF26FD058A6765DB3A66A85 Content-Type: message/rfc822; name="Re: hidden static.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: hidden static.eml"
Path: reader2.panix.com!panix!goblin2!goblin1!goblin.stu.neva.ru!border1.nntp.ams1.giganews.com!nntp.giganews.com!buffer1.nntp.ams1.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Sat, 19 Nov 2016 16:12:33 -0600 Date: Sun, 20 Nov 2016 00:12:36 +0200 From: Paavo Helde User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.8) Gecko/20151117 FossaMail/25.1.9 MIME-Version: 1.0 Newsgroups: comp.lang.c++ Subject: Re: hidden static References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID: X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-5Y0ca5EXbUi1Ofd9NOQ7/KALrzYewxQOY1KKx+9Sv3gUeMq2wkBv4dAEdpT3bxMO5lJTn5lGmzP4IzY!mUAcuwRUtXz1lofm+Vu09FPPm44+L4MWY2OB/rCiaLJZ24jkkzEDQTl9C36LmnAQMPYxYxCbG/Y= X-Complaints-To: abuse-at-giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2479 Xref: panix comp.lang.c++:1125350
On 19.11.2016 19:01, Popping mad wrote: > On Sat, 19 Nov 2016 10:30:14 +0200, Paavo Helde wrote: > >> See https://isocpp.org/wiki/faq/ctors#link-errs-static-data-mems >> >> While there, consider reading the whole https://isocpp.org/faq > > > This is an excellent explanation, but I'm still not clear on this. I > think it is saying that a static has to be declared and defined within a > compilational unit. I'm not sure why, though.
There are other things in C++ which are declared in a header file and then defined in some translation unit. The prime example are functions (member and non-member). That's just the way how C++ does things.
When defining a class static member in some cpp file, one tells the compiler to reserve memory for it in an the data segment of this translation unit, plus one also specifies the arguments instructing the compiler how to construct it.
However, for some simpler scenarios the initialization of the class static member can be written directly in the declaration, which is probably the most confusing point here.
> And I've asked before, > and read, but I'm still not clear on what a compilation unit is.
It is just whatever is compiled into a single object .o/.obj file.
hth Paavo
--------------FFF26FD058A6765DB3A66A85 Content-Type: message/rfc822; name="Re: hidden static.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: hidden static.eml"
Path: reader2.panix.com!panix!goblin3!goblin.stu.neva.ru!news.netfront.net!.POSTED.217.30.184.161!not-for-mail From: Juha Nieminen Newsgroups: comp.lang.c++ Subject: Re: hidden static Date: Mon, 21 Nov 2016 07:52:39 +0000 (UTC) Organization: Netfront http://www.netfront.net/ Message-ID: References: Injection-Date: Mon, 21 Nov 2016 07:52:39 +0000 (UTC) Injection-Info: adenine.netfront.net; posting-host="217.30.184.161"; logging-data="34748"; mail-complaints-to="news-at-netfront.net" User-Agent: tin/2.2.1-20140504 ("Tober an Righ") (UNIX) (Linux/3.14.79-grbfs-kapsi (x86_64)) Xref: panix comp.lang.c++:1125395
Popping mad wrote: > This is an excellent explanation, but I'm still not clear on this. I > think it is saying that a static has to be declared and defined within a > compilational unit. I'm not sure why, though. And I've asked before, > and read, but I'm still not clear on what a compilation unit is.
A static member variable needs to exist somewhere in the executable (because, among other things, you need to be able to eg. have a pointer pointing to it). You have to tell the compiler which compilation unit (in practice which object file) the actual instance of that variable is. The compiler won't guess; you need to tell it explicitly where it should be put.
(Incidentally, C++17 will introduce the concept of inline variables, which will allow you to define static member variables in the header file itself, causing the linker to merge all the instantiations into one. But that will be available only after compilers start supporting it.)
--------------FFF26FD058A6765DB3A66A85 Content-Type: message/rfc822; name="Re: hidden static.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: hidden static.eml"
Path: reader2.panix.com!panix!goblin2!goblin.stu.neva.ru!peer02.am4!peer.am4.highwinds-media.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!post02.iad.highwinds-media.com!fx34.iad.POSTED!not-for-mail X-Newsreader: xrn 9.03-beta-14-64bit Sender: scott-at-dragon.sl.home (Scott Lurndal) From: scott-at-slp53.sl.home (Scott Lurndal) Reply-To: slp53-at-pacbell.net Subject: Re: hidden static Newsgroups: comp.lang.c++ References: Message-ID: X-Complaints-To: abuse-at-usenetserver.com NNTP-Posting-Date: Mon, 21 Nov 2016 13:26:07 UTC Organization: UsenetServer - www.usenetserver.com Date: Mon, 21 Nov 2016 13:26:07 GMT X-Received-Body-CRC: 1994721708 X-Received-Bytes: 1585 Xref: panix comp.lang.c++:1125403
Juha Nieminen writes: >Popping mad wrote: >> This is an excellent explanation, but I'm still not clear on this. I >> think it is saying that a static has to be declared and defined within a >> compilational unit. I'm not sure why, though. And I've asked before, >> and read, but I'm still not clear on what a compilation unit is. > >A static member variable needs to exist somewhere in the executable >(because, among other things, you need to be able to eg. have a pointer >pointing to it).
The confusing part for some folks is that gcc/binutils, when using -O3, won't require the definition unless the address of the static const value is taken. Remove -O3 and you'll start getting link errors.
--------------FFF26FD058A6765DB3A66A85 Content-Type: message/rfc822; name="Re: hidden static.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: hidden static.eml"
Path: reader2.panix.com!panix!goblin2!goblin1!goblin.stu.neva.ru!border1.nntp.ams1.giganews.com!nntp.giganews.com!buffer1.nntp.ams1.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail NNTP-Posting-Date: Mon, 21 Nov 2016 08:48:30 -0600 Date: Mon, 21 Nov 2016 14:48:15 +0000 From: Chris Vine Newsgroups: comp.lang.c++ Subject: Re: hidden static Message-ID: <20161121144815.63a2899c-at-dell.homenet> References: X-Newsreader: Claws Mail 3.13.2 (GTK+ 2.24.31; x86_64-unknown-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-w23lfhOSTorUVxFJLxf9GJg1mUExKYcD2Xk+4+sbdp5V3lpWUJhhVbK9gPcC7+oxblQ7q5+aeOZgYc2!0+6DIetz8FWHSoHw+gggdCyZb2FNzSyHzu1tn9kx/qAAT8sdbrcMPN/2xbnPjX6tcPA+GNMSXxSU!RDVnGOyi X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2575 Xref: panix comp.lang.c++:1125407
On Mon, 21 Nov 2016 13:26:07 GMT scott-at-slp53.sl.home (Scott Lurndal) wrote: > Juha Nieminen writes: > >Popping mad wrote: > >> This is an excellent explanation, but I'm still not clear on > >> this. I think it is saying that a static has to be declared and > >> defined within a compilational unit. I'm not sure why, though. > >> And I've asked before, and read, but I'm still not clear on what a > >> compilation unit is. > > > >A static member variable needs to exist somewhere in the executable > >(because, among other things, you need to be able to eg. have a > >pointer pointing to it). > > The confusing part for some folks is that gcc/binutils, when using > -O3, won't require the definition unless the address of the static > const value is taken. Remove -O3 and you'll start getting link > errors.
My recollection in the case of the declaration of a const static integer member variable (typically with a literal value) is that you don't need to provide a definition for it if it is not ODR-used, whatever the optimization level. Taking an address is one way of ODR-using it. Casting it to lvalue of another type is another. I don't at the moment have the C++ standard in front of me to give you the reference.
These days I guess you would probably declare such a static member variable constexpr rather than const anyway.
--------------FFF26FD058A6765DB3A66A85 Content-Type: message/rfc822; name="Re: hidden static.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: hidden static.eml"
Path: reader2.panix.com!panix!bloom-beacon.mit.edu!bloom-beacon.mit.edu!168.235.88.217.MISMATCH!2.us.feeder.erje.net!feeder.erje.net!2.eu.feeder.erje.net!border1.nntp.ams1.giganews.com!border2.nntp.ams1.giganews.com!nntp.giganews.com!buffer2.nntp.ams1.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail NNTP-Posting-Date: Mon, 21 Nov 2016 11:31:15 -0600 Date: Mon, 21 Nov 2016 17:31:03 +0000 From: Chris Vine Newsgroups: comp.lang.c++ Subject: Re: hidden static Message-ID: <20161121173103.17c0ed02-at-dell.homenet> References: <20161121144815.63a2899c-at-dell.homenet> X-Newsreader: Claws Mail 3.13.2 (GTK+ 2.24.31; x86_64-unknown-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-oGEaznJ0WmZ0WU74CQDIc7tWSUSBoYvtVrKMYhPBRiE4+715VbCH+RDVVNEJAs2DMiR2d+aFj+wJCA7!odNsSDggeWVOOEhMOjuXXgq/m+7uSa9VsyWrEaZdU2o0DI2oOnOca72eX4QQ8AiBHzdvU38CP5qw!GKQHgRdXTw== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 Bytes: 3391 X-Original-Bytes: 3329 Xref: panix comp.lang.c++:1125410
On Mon, 21 Nov 2016 14:48:15 +0000 Chris Vine wrote: > On Mon, 21 Nov 2016 13:26:07 GMT > scott-at-slp53.sl.home (Scott Lurndal) wrote: > > Juha Nieminen writes: =20 > > >Popping mad wrote: =20 > > >> This is an excellent explanation, but I'm still not clear on > > >> this. I think it is saying that a static has to be declared and > > >> defined within a compilational unit. I'm not sure why, though. > > >> And I've asked before, and read, but I'm still not clear on what > > >> a compilation unit is. =20 > > > > > >A static member variable needs to exist somewhere in the executable > > >(because, among other things, you need to be able to eg. have a > > >pointer pointing to it). =20 > >=20 > > The confusing part for some folks is that gcc/binutils, when using > > -O3, won't require the definition unless the address of the static > > const value is taken. Remove -O3 and you'll start getting link > > errors. =20 >=20 > My recollection in the case of the declaration of a const static > integer member variable (typically with a literal value) is that you > don't need to provide a definition for it if it is not ODR-used, > whatever the optimization level. Taking an address is one way of > ODR-using it. Casting it to lvalue of another type is another. I > don't at the moment have the C++ standard in front of me to give you > the reference.
I do now. It's =C2=A79.4.2/3. "If a non-volatile const static data member is of integral or enumeration type, its declaration in the class definition can specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression (5.20). ... . The member shall still be defined in a namespace scope if it is odr-used (3.2) in the program and the namespace scope definition shall not contain an initializer." This implies that the initialised const static integral member need not be defined in namespace scope (its declaration is sufficient) if it is not ODR-used.
Chris
--------------FFF26FD058A6765DB3A66A85 Content-Type: message/rfc822; name="Re: hidden static.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: hidden static.eml"
Path: reader2.panix.com!panix!goblin1!goblin.stu.neva.ru!border1.nntp.ams1.giganews.com!nntp.giganews.com!buffer1.nntp.ams1.giganews.com!buffer2.nntp.ams1.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail NNTP-Posting-Date: Mon, 21 Nov 2016 12:18:19 -0600 Date: Mon, 21 Nov 2016 18:18:06 +0000 From: Chris Vine Newsgroups: comp.lang.c++ Subject: Re: hidden static Message-ID: <20161121181806.58043cf8-at-dell.homenet> References: <20161121144815.63a2899c-at-dell.homenet> <20161121173103.17c0ed02-at-dell.homenet> X-Newsreader: Claws Mail 3.13.2 (GTK+ 2.24.31; x86_64-unknown-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-JEQj3mz1xKbQAxdXsTVYjiLyAGHuskHPx6rcdgJiKG1th3pQdemvzbHcbbPtRmWZPHdGZMvySdw9xkp!zJKS8AK8oubU+tgV9K6rBBOpBUduh0u5A/WvZrAjJaJGhjPcCdzUF/VwIUXFRpwZpRShEwu3uDOq!Rd2aFv50mw== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3328 Xref: panix comp.lang.c++:1125411
On Mon, 21 Nov 2016 17:31:03 +0000 Chris Vine wrote: > On Mon, 21 Nov 2016 14:48:15 +0000 > Chris Vine wrote: > > On Mon, 21 Nov 2016 13:26:07 GMT > > scott-at-slp53.sl.home (Scott Lurndal) wrote: =20 [snip] > > > The confusing part for some folks is that gcc/binutils, when using > > > -O3, won't require the definition unless the address of the static > > > const value is taken. Remove -O3 and you'll start getting link > > > errors. =20 > >=20 > > My recollection in the case of the declaration of a const static > > integer member variable (typically with a literal value) is that you > > don't need to provide a definition for it if it is not ODR-used, > > whatever the optimization level. Taking an address is one way of > > ODR-using it. Casting it to lvalue of another type is another. I > > don't at the moment have the C++ standard in front of me to give you > > the reference. =20 >=20 > I do now. It's =C2=A79.4.2/3. "If a non-volatile const static data memb= er > is of integral or enumeration type, its declaration in the class > definition can specify a brace-or-equal-initializer in which every > initializer-clause that is an assignment-expression is a constant > expression (5.20). ... . The member shall still be defined in > a namespace scope if it is odr-used (3.2) in the program and the > namespace scope definition shall not contain an initializer." This > implies that the initialised const static integral member need not be > defined in namespace scope (its declaration is sufficient) if it is > not ODR-used.
However I do now recall having experiences similar to the one you mention. Some forms of ODR-use, such as assigning to a const reference, may be optimized out with a sufficiently high level of optimization, and will compile and link even though technically the code in question is not conformant. That may be what you had in mind. But where the static member is not ODR-used, you should be safe from that problem.
--------------FFF26FD058A6765DB3A66A85 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline
_______________________________________________ Learn mailing list Learn-at-nylxs.com http://lists.mrbrklyn.com/mailman/listinfo/learn
--------------FFF26FD058A6765DB3A66A85--
|
|