Re: 14/8 says my code is right, compiler says it's wrong
"Alberto Ganesh Barbati" <AlbertoBarbati@libero.it> schrieb im Newsbeitrag
news:2aZVg.139305$_J1.908965@twister2.libero.it...
The "Error" line implicitly instantiates the traits template, according
to ?14.7.1/1, so the code is ill-formed per ?14.7.1.2/6: "If an implicit
instantiation of a class template specialization is required and the
template is declared but not defined, the program is ill-formed."
So the template must also be *defined*, but where? Does it have to be
defined before any implicit instantiation? In any translation unit where
such an implicit instantiation takes place, or just in one?
Notice that the error occurs in main.cpp and the presence of the
explicit specialization in item.cpp (a different compilation unit) is
not enough because of ?14.7.3/6: "If a template, a member template or
the member of a class template is explicitly specialized then that
specialization shall be declared before the first use of that
specialization that would cause an implicit instantiation to take place,
in every translation unit in which such a use occurs; no diagnostic is
required."
So the specialization must be declared before any implicit instantiation,
but what about the definition?
Notice that, in this particular case, you can't just declare the
specialization item_traits<Item<int>>, because in order to use foo() you
also need the declaration of that! So you must provide the entire
definition of item_traits<Item<int>> in item.h (you don't need to
provide the definition of foo(), however).
Where does the standard say that I need to provide the entire definition
before the implicit instantiation?
I would loathe to provide the definition of the template specialization in a
header file, because my intent is to separate interface and implementation.
In my example, "item.h" would be included in many translation units, so any
change to the template specialization would require recompilation of each of
these translation units.
--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Toilet Tycoon
http://www.anvil-soft.de - Die Macher des Klomanagers
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]