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