Re: 14/8 says my code is right, compiler says it's wrong
"Alberto Ganesh Barbati" <AlbertoBarbati@libero.it> schrieb im Newsbeitrag
news:2TvWg.141171$_J1.912671@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."
[snip]
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?
?14.7.1.2/6... did you read my post?
Yes, I did. But your quote of 14.7.1.2/6 only *requires* a definition,
without saying *where* it should be made.
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.
You don't need to define all member function, just the class definition
is enough. For example:
---- item.h
template <class T> struct item_traits;
template <class T> class Item; // forward declaration
// definition of item_traits< Item<int> >
template <> struct item_traits< Item<int> >
{
static int foo(); // declaration of foo()
};
template <class T> class Item
{
public:
typedef item_traits< Item<T> > traits_type;
int foo() { return traits_type::foo(); }
};
---- item.cpp
#include "item.h"
int item_traits< Item<int> >::foo() { return 42; }
Oh, I see. That's good news. But doesn't this require the notoriously
unsupported 'export' feature? I vaguely remember that there is an exception
for template *specializations*, though...
--
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! ]