From: matt on 8 Mar 2010 02:15 Hi all, I was reading in Bruce Eckel's Thinking in C++, 2nd Ed Vol1, Ch 8 about extern and const. He states that: "To use const instead of #define, you must be able to place const definitions inside header files as you can with #define. This way, you can place the definition for a const in a single place and distribute it to translation units by including the header file. A const in C++ defaults to internal linkage; that is, it is visible only within the file where it is defined and cannot be seen at link time by other translation units." However, I wrote a short test program in gcc 4.4.1 that defines a non- extern constant within a header file as: const double myConst = 3.14159; This header is included by another file, which then uses the value of myConst without problem. The fact that this is allowed seems to contradict the assertions made by Eckel. So, is gcc allowing this although it shouldn't? Or is Mr. Eckel's statement incorrect? Or have I misunderstood something? Thanks, Matt. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Thomas Richter on 8 Mar 2010 05:05 matt wrote: > Hi all, > > I was reading in Bruce Eckel's Thinking in C++, 2nd Ed Vol1, Ch 8 > about extern and const. He states that: > > "To use const instead of #define, you must be able to place const > definitions inside header files as you can with #define. This way, you > can place the definition for a const in a single place and distribute > it to translation units by including the header file. A const in C++ > defaults to internal linkage; that is, it is visible only within the > file where it is defined and cannot be seen at link time by other > translation units." > > However, I wrote a short test program in gcc 4.4.1 that defines a non- > extern constant within a header file as: > > const double myConst = 3.14159; > > This header is included by another file, which then uses the value of > myConst without problem. The fact that this is allowed seems to > contradict the assertions made by Eckel. The definition of myConst is internal to the *translation unit* (which is not a single file!) - simply because you include the header file; it becomes internal to all the files that include it and are compiled as independently. Thus, your confusion is likely that you confuse files with translation units. What you cannot do in C++ is to write one .cpp file that does include the header, compile it separately, then in a second .cpp file that does *not* include the header write: extern const double myConst; and expect it to compile. In plain C, you could, in C++, you can't, unless you write an "extern" in front of the definition. Greetings, Thomas -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Daniel Krügler on 8 Mar 2010 05:06 On 8 Mrz., 20:15, matt <li...(a)givemefish.com> wrote: > I was reading in Bruce Eckel's Thinking in C++, 2nd Ed Vol1, Ch 8 > about extern and const. He states that: > > "To use const instead of #define, you must be able to place const > definitions inside header files as you can with #define. This way, you > can place the definition for a const in a single place and distribute > it to translation units by including the header file. A const in C++ > defaults to internal linkage; that is, it is visible only within the > file where it is defined and cannot be seen at link time by other > translation units." > > However, I wrote a short test program in gcc 4.4.1 that defines a non- > extern constant within a header file as: > > const double myConst = 3.14159; > > This header is included by another file, which then uses the value of > myConst without problem. The fact that this is allowed seems to > contradict the assertions made by Eckel. > > So, is gcc allowing this although it shouldn't? Or is Mr. Eckel's > statement incorrect? Or have I misunderstood something? The latter. By including your header (let's name it H) in any other file the variable is visible within the resulting translation unit (lets name it TU1). But if you include H in another file that belongs to another translation unit TU2, the myConst in TU1 coexists with another myConst in TU2. Neither can TU1 name "TU2::myConst" (this is only a symbolic qualification, it is not part of the C++ language) nor can TU2 name "TU1::myConst". If this would not be the case, the program would cause a linkage error, because the same entity would be multiply defined. With internal linkage you can define multiple objects with the same name *within* each translation unit. External variables (including constants with external linkage) can only be defined once per program without violating the one-definition rule. HTH & Greetings from Bremen, Daniel Kr�gler -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Arijit on 8 Mar 2010 05:06 On Mar 9, 12:15 am, matt <li...(a)givemefish.com> wrote: > Hi all, > [snip] > const double myConst = 3.14159; > > This header is included by another file, which then uses the value of > myConst without problem. The fact that this is allowed seems to > contradict the assertions made by Eckel. > > So, is gcc allowing this although it shouldn't? Or is Mr. Eckel's > statement incorrect? Or have I misunderstood something? consts do default to internal linkage. What command line are you using to invoke gcc ? I suspect you are using the C compiler instead of c++, since in C consts have external linkage. Regards Arijit -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Seungbeom Kim on 8 Mar 2010 05:05
matt wrote: > > I was reading in Bruce Eckel's Thinking in C++, 2nd Ed Vol1, Ch 8 > about extern and const. He states that: > > "To use const instead of #define, you must be able to place const > definitions inside header files as you can with #define. This way, you > can place the definition for a const in a single place and distribute > it to translation units by including the header file. A const in C++ > defaults to internal linkage; that is, it is visible only within the > file where it is defined and cannot be seen at link time by other > translation units." > > However, I wrote a short test program in gcc 4.4.1 that defines a non- > extern constant within a header file as: > > const double myConst = 3.14159; > > This header is included by another file, which then uses the value of > myConst without problem. The fact that this is allowed seems to > contradict the assertions made by Eckel. It doesn't. If multiple translation units include the header file, each of them defines its own variable with the same name, which "cannot be seen at link time by other translation units". On the other hand, if the linkage were external, the variable could be seen in other translation units which defined its own variable with the same name, and the link would fail because of the name clash. -- Seungbeom Kim [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |