14/8 says my code is right, compiler says it's wrong
Hello everybody!
I have got some code which is supposed to compile, but it doesn't. My
assumption that it "is supposed to compile" is based on 14/8 of the 2003 C++
standard, which says: "A non-exported template must be defined in every
translation unit in which it is implicitly instantiated (14.7.1), unless the
corresponding specialization is explicitly instantiated (14.7.2) in some
translation unit;[...]". The important part of this sentence is the second
one, which says, according to my understanding, that if a (non-exported)
template is explicitly specialized, it only needs to be instantiated in
"some" translation unit. Now let's look at my code:
// ---------- begin file "item.h" ----------
template <class T> struct item_traits;
template <class T> class Item
{
public:
typedef item_traits< Item<T> > traits_type;
int foo() { return traits_type::foo(); } // Error C2027
};
// ---------- end file "item.h" ----------
// ---------- begin file "item.cpp"----------
#include "item.h"
template <class T> struct item_traits {};
template <> struct item_traits< Item<int> >
{
static int foo() { return 42; }
};
// ---------- end file "item.cpp" ----------
// ---------- begin file "main.cpp" ----------
#include <iostream>
#include "item.h"
int main()
{
Item<int> x;
std::cout << x.foo() << std::endl;
return 0;
}
// ---------- end file "main.cpp" ----------
When I compile this on Visual C++ 2005 Express Edition, I get an error C2027
for using an undefined type "item_traits<T>" with "T=Item<int>" in file
"item.h" for the line that says:
int foo() { return traits_type::foo(); } // Error C2027
Note that item_traits< Item<int> > is explicitly specialized in file
"item.cpp", and the passage quoted from the standard at the beginning of
this post seems to says that this is all that's needed. What's wrong here?
--
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! ]