Topic: Multiple definitions of objects/functions not used


Author: James Kanze <james.kanze@gmail.com>
Date: Mon, 11 May 2009 13:20:24 CST
Raw View
On May 11, 8:47 am, litb <Schaub-Johan...@web.de> wrote:
> Hello all. Up to now, i thought the ODR rules when and when
> not an object/non-inline function can be defined multiple
> times. It says at 3.2/3:

> "Every program shall contain exactly one de      nition of every
> non-inline function or object that is used in that program; no
> diagnostic required. The de      nition can appear explicitly in
> the program, it can be found in the standard or a user-de      ned
> library, or (when appropriate) it is implicitly de      ned (see
> 12.1, 12.4 and 12.8). An inline function shall be de      ned in
> every translation unit in which it is used."

> Why is it only restricted to objects or non-inline functions
> that are used?

Because there's no reason for requiring a definition of a
function or object which isn't used.

> I would have expected the following wording:

> "Every program shall contain at most one definition of every
> non- inline function or object, but a definition is required
> if the entity is used in that program; no diagnostic required.
> The definition can appear explicitly in the program, it can be
> found in the standard or a user-defined library, or (when
> appropriate) it is implicitly defined (see 12.1, 12.4 and
> 12.8). An inline function shall be defined in every
> translation unit in which it is used."

> What is the rationale for restricting to only used functions/objects?

As above: there's no reason for requiring a definition of a
function or object which isn't used.

> Is it an oversight, or are there rules on other places forcing
> at most one definition, maybe in the linkage sections? Apart
> from that, i believe most compilers/linkers moan anyway, even
> if the function or object is not used.

I think you do have a point.  Providing multiple definitions of
a function or object will typically cause translation to fail in
the link phase, even if the function or object isn't used.  Off
hand, I can't see anything to forbid:

source1.cc:

     extern void f() ;
     extern void g() ;

     int
     main()
     {
         f() ;
         g() ;
         return 0 ;
     }

source2.cc:

     void f() {}
     void h() {}

source3.cc:

     void g() {}
     void h() {}

I don't think it was the intent to allow it, however, and it
does fail to translate (error from the linker) with both g++ and
VC++.

I'd say it was a defect in the standard (rather than an almost
error in all, or almost all compilers).

--
James Kanze (GABI Software)             email:james.kanze@gmail.com
Conseils en informatique orient    e objet/
                    Beratung in objektorientierter Datenverarbeitung
9 place S    mard, 78210 St.-Cyr-l'    cole, France, +33 (0)1 30 23 00 34


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: litb <Schaub-Johannes@web.de>
Date: Wed, 13 May 2009 10:40:00 CST
Raw View
On 11 Mai, 21:20, James Kanze <james.ka...@gmail.com> wrote:
> On May 11, 8:47 am, litb <Schaub-Johan...@web.de> wrote:
>
> > Hello all. Up to now, i thought the ODR rules when and when
> > not an object/non-inline function can be defined multiple
> > times. It says at 3.2/3:
> > "Every program shall contain exactly one de    nition of every
> > non-inline function or object that is used in that program; no
> > diagnostic required. The de    nition can appear explicitly in
> > the program, it can be found in the standard or a user-de    ned
> > library, or (when appropriate) it is implicitly de    ned (see
> > 12.1, 12.4 and 12.8). An inline function shall be de    ned in
> > every translation unit in which it is used."
> > Why is it only restricted to objects or non-inline functions
> > that are used?
>
> Because there's no reason for requiring a definition of a
> function or object which isn't used.
>

Yeah, i was aware of that. I should have worded the question better.
Next time, i'll keep an eye on that.

> > Is it an oversight, or are there rules on other places forcing
> > at most one definition, maybe in the linkage sections? Apart
> > from that, i believe most compilers/linkers moan anyway, even
> > if the function or object is not used.
>
> I think you do have a point.  Providing multiple definitions of
> a function or object will typically cause translation to fail in
> the link phase, even if the function or object isn't used.
>

I found that it says in essence the same as my proposed wording in 9.3
[class.mfct] paragraph 4, but only talking about member-functions.
Should i resend this question now using the "Defect Report: ..."
guideline and with "xy [...] para" style refs? I'm not sure how to
proceed, not having much experience with usenet yet :)


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: James Kanze <james.kanze@gmail.com>
Date: Thu, 14 May 2009 13:13:29 CST
Raw View
On May 13, 6:40 pm, litb <Schaub-Johan...@web.de> wrote:
> On 11 Mai, 21:20, James Kanze <james.ka...@gmail.com> wrote:

      [...]
> > > Is it an oversight, or are there rules on other places
> > > forcing at most one definition, maybe in the linkage
> > > sections? Apart from that, i believe most
> > > compilers/linkers moan anyway, even if the function or
> > > object is not used.

> > I think you do have a point.  Providing multiple definitions
> > of a function or object will typically cause translation to
> > fail in the link phase, even if the function or object isn't
> > used.

> I found that it says in essence the same as my proposed
> wording in 9.3 [class.mfct] paragraph 4, but only talking
> about member-functions.  Should i resend this question now
> using the "Defect Report: ..." guideline and with "xy [...]
> para" style refs? I'm not sure how to proceed, not having much
> experience with usenet yet :)

It wouldn't hurt.  With an example of something which won't
compile (link), although there's nothing in the standard which
forbids it, so that it's clear where the error lies.

--
James Kanze (GABI Software)             email:james.kanze@gmail.com
Conseils en informatique orient   e objet/
                    Beratung in objektorientierter Datenverarbeitung
9 place S   mard, 78210 St.-Cyr-l'   cole, France, +33 (0)1 30 23 00 34


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: litb <Schaub-Johannes@web.de>
Date: Mon, 11 May 2009 00:47:48 CST
Raw View
Hello all. Up to now, i thought the ODR rules when and when not an
object/non-inline function can be defined multiple times. It says at
3.2/3:

"Every program shall contain exactly one de   nition of every non-inline
function or object that is used in that program; no diagnostic
required. The de   nition can appear explicitly in the program, it can
be found in the standard or a user-de   ned library, or (when
appropriate) it is implicitly de   ned (see 12.1, 12.4 and 12.8). An
inline function shall be de   ned in every translation unit in which it
is used."

Why is it only restricted to objects or non-inline functions that are
used? I would have expected the following wording:

"Every program shall contain at most one definition of every non-
inline function or object, but a definition is required if the entity
is used in that program; no diagnostic required. The definition can
appear explicitly in the program, it can be found in the standard or a
user-defined library, or (when appropriate) it is implicitly defined
(see 12.1, 12.4 and 12.8). An inline function shall be defined in
every translation unit in which it is used."

What is the rationale for restricting to only used functions/objects?
Is it an oversight, or are there rules on other places forcing at most
one definition, maybe in the linkage sections? Apart from that, i
believe most compilers/linkers moan anyway, even if the function or
object is not used.

Thanks


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]