Re: Are unused friend functions compiled or not?
Walter Bright wrote:
Consider the code:
-----------------
template <class T>
struct addable2
{
friend T operator +( T lhs, const T& rhs ) { return lhs += rhs; }
};
template<class T>
struct addable : addable2<T>
{
};
template <class T>
class point : addable< point<T> >
{
};
int main()
{
point<int> ppp;
return 0;
}
-------------------
operator+ has a semantic error in it, there is no += operation defined
for point<int>. So should an error be issued or not?
No. This is a widespread and well-known technique.
The Standard 14.5.3-3 says:
It's in 14.5.3/5 in my copy.
3. When a function is defined in a friend function declaration in a
class template, the function is defined when
the class template is first instantiated. The function is defined even
if it is never used. [Note: if the function
definition is illformed
for a given specialization of the enclosing class template, the program
is illformed even if the function is never used. -end note]
This is an obvious error in the standard (although I can't image
how so blatant an error came about). It is in contradiction
with the basic principles governing instantiation elsewhere, and
with use that the committee explicitly wanted to support.
(There is a special rule concerning name lookup to ensure that
the operator+ will be found by ADL, for example; second point in
'3.4.2/4.)
A fairly recent draft (N2009) says ('14.5.3/4): "When a function
is defined in a friend function declaration in a class template,
the function is instantiated when the function is used." This
is the result of DR 329. (Note that John Spicer's description
in the DR list is very succinct, and doesn't mention the actual
use, which corresponds to the above in Boost---and in my own
code.)
Note too that the statement you cite is in direct contradiction
with '14.7.1/2 "[...], the function template specialization is
implicitly instantiated when the specicalization is referenced
in a context that requires a function definition to exist.",
then in '14.7.1/9: "An imlementation shall not implicitly
instantiate a function template, a member template [...] that
does not require instantiation." Regretfully, the list in the
second sentence doesn't include friend functions (although I
think that would have been the intent), and the first sentence I
quote doesn't really exclude other instantiations. And of
course, a specification for a specific case generally has
precedance over more general statements. Still, the above
statements express the general philosophy concerning
instantiation, and I would find it surprising if friend
functions didn't obey the same rules.
This came about from trying to compile Boost code with Digital Mars C++.
Evidently, such code is common in Boost. But I feel that DMC++ correctly
rejects the code.
Unless there are still other sections we haven't seen, it
certainly should reject it according to ISO 14882:1198, as
written. On the other hand, you're certainly aware that this
version of the standard contained an error or two, some of which
have since been corrected:-). (I'm not sure that this could be
considered a real error. According to John---and he should
know---the original wording was intentional, and corresponded to
existing practice in 7/1996. My somewhat vague memory is that
back that far, most compilers systematically instantiated all of
the definitions in a class whenever they instantiated the class,
however, and the standard explicitly broke with that policy for
non-virtual member functions. But it doesn't really matter; the
current requirements are clear.)
I might add that if both g++ and Microsoft agree, that probably
carries more weight than the standard in practice:-).
(Realistically, if you're selling compilers to the Windows
market, you probably need a "Microsoft bug-compatible" option
anyway. Not saying that that's the way it should be, of course.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientie objet/
Beratung in objektorientierter Datenverarbeitung
9 place Simard, 78210 St.-Cyr-l'Icole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]