Re: , definition of "used" for templates
Am 08.10.2010 15:48, schrieb Johannes Schaub (litb):
[..]
More interestingly, what about this one?
struct Printer { Printer() { std::cout<< "I do print!"<<
std::endl; } };
template<typename T> struct A { static Printer printer; };
template<typename T> Printer A<T>::printer;
template<typename T> void f() { &A<void>::printer; }
int main() { }
If that line "uses" the printer because it appears in a potentially
evaluated expression, it shall print "I do print!". I tested this on
both GCC and Clang, both of which do *not* print "I do print!".
So it appears GCC and Clang disagree with your reading of the ODR.
I don't think that they disagree. The wording of the combination
of 3.2 and 14.7.1 is sufficiently diffuse to give a lot of
wiggle room for implementations. For a user it means that the
variable is potentially used, for an implementation the wording
allows to not instantiate A<void> *and* not to use A<void>::printer
(Note my emphasize that both decisions are independent ones).
The wording basically allows an implementation to "rewrite" f()
as follows:
void __invoker() { // Existing as a single definition
&A<void>::printer;
}
template<typename T>
void f() {
__invoker();
}
and to use this as an opportunity to prevent multiple instantiations
of &A<void>::printer when f() *would* be instantiated multiple
times in another program - it's just so that most (all?)
implementations don't do that this way.
I certainly agree that the current situation is quite pessimistic
for a user and especially the combination of ODR and template
instantiation rooms should be more clearly defined.
HTH & Greetings from Bremen,
Daniel Kr??gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]