469,934 Members | 1,824 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

Re: Inconsistent template behavior; standard-conforming, UB, or gccbug?

Paavo Helde wrote:
So you have to declare all bar() overloads before your template
definitions, and I bet the visible declarations in both files have to be
the same to avoid UB.
So which compiler is correct, gcc (which only requires bar() to be
declared at the point of insantiation of foo()) or comeau?

What happens if you just move the bar() function to be before the
foo() function in both source files? (It's not like this was critical
from the point of view of my original post.)
Jun 27 '08 #1
2 1410
On May 16, 10:07 am, Juha Nieminen <nos...@thanks.invalidwrote:
Paavo Helde wrote:
So you have to declare all bar() overloads before your template
definitions, and I bet the visible declarations in both files have to be
the same to avoid UB.
So which compiler is correct, gcc (which only requires bar() to be
declared at the point of insantiation of foo()) or comeau?
Comeau. It's more subtle that one might suspect from Paavo's
posting, but this is an error in g++ (at least in version
4.1.0).
What happens if you just move the bar() function to be before
the foo() function in both source files? (It's not like this
was critical from the point of view of my original post.)
It's undefined behavior, and at least with the version of g++ I
have handy, and my usual compile flags, depends on the
optimization level. In your code, it's clear that:

1. you have two different functions bar,
2. the instantiaion foo<inttakes place at several different
places, and
3. the name bar in this instantiation is bound to a different
function, depending on the point of instantiation.

Point 3 leads to undefined behavior, according to the last
paragraph of §14.6.4.1:

A specialization for a function template, a member
function template, or of a member function or static
data member of a class template may have multiple points
of instantiations within a translation unit. A
specialization for a class template has at most one
point of instantiation within a translation unit. A
specialization for any template may have points of
instantiation in multiple translation units. If two
different points of instantiation give a template
specialization different meanings according to the one
definition rule (3.2), the program is ill-formed, no
diagnostic required.

--
James Kanze (GABI Software) email:ja*********@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
Jun 27 '08 #2
James Kanze wrote:
Point 3 leads to undefined behavior, according to the last
paragraph of §14.6.4.1:

A specialization for a function template, a member
function template, or of a member function or static
data member of a class template may have multiple points
of instantiations within a translation unit. A
specialization for a class template has at most one
point of instantiation within a translation unit. A
specialization for any template may have points of
instantiation in multiple translation units. If two
different points of instantiation give a template
specialization different meanings according to the one
definition rule (3.2), the program is ill-formed, no
diagnostic required.
Thanks. That was helpful information.
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Senthilvel | last post: by
reply views Thread by ckhoge | last post: by
12 posts views Thread by mlimber | last post: by
2 posts views Thread by Glenn G. Chappell | last post: by
1 post views Thread by Peter Knörrich | last post: by
19 posts views Thread by n.torrey.pines | last post: by
4 posts views Thread by stinos | last post: by
20 posts views Thread by Francine.Neary | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.