This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: One-definition rule for inline functions
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Timothy Madden <terminatorul at gmail dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Thu, 8 Mar 2012 01:10:50 +0000
- Subject: Re: One-definition rule for inline functions
- References: <jj90f0$5qh$1@dough.gmane.org>
On 8 March 2012 01:02, Timothy Madden wrote:
> Hello
>
> I have an inline function like this:
>
> void draw_unit_vector()
> {
> ? ? ? ?// some large code here
> ? ? ? ?// ...
>
> ? ? ? ?LineTo(1, 1);
> }
>
> in two translation units A and B.
>
> Now if the function is large enough it is still compiled and called, and not
> inlined.
>
> The LineTo() function has two overloads:
> ? ? ? ?- translation unit A only needs the float version:
> ? ? ? ? ? ? ? ? ?LineTo(float x, float y);
> ? ? ? ? ?and declares it before it includes the inline function.
> ? ? ? ?- translation unit B uses both int and float versions:
> ? ? ? ? ? ? ? ? ?LineTo(int x, int y);
> ? ? ? ? ? ? ? ? ?LineTo(float x, float y);
> ? ? ? ? ?and declares them before it includes the inline function.
>
> The same inline function would call LineTo(float, float) in source A, but
> would call LineTo(int, int) in source B. And it so happens that the two
> overloads for LineTo() are quite different, the int version is for text-mode
> only, while the float version is for graphics mode.
>
> By the C++ standard this case is a clear violation of the one-definition
> rule, and users should take good care to avoid it.
>
> My question is: what does g++ do in such case ?
>
> Does it compile two different draw_unit_vector() functions from the same
> inline function definition ?
Yes, then discards one of the definitions, see
http://gcc.gnu.org/onlinedocs/gcc/Vague-Linkage.html