T
truce
I searched but couldn't find anything _quite_ like this. I have some
code I inherited that needs some work. Basically I have a template
that is instantiated in the c++ code and so only the headers are
distributed. That works great, the only problem is that I cant get
the friend ostream inserter operator to work correctly. Basically
it never is instantiated so it never shows up in the library. Here is
a (close to) minimum example demonstrating the problem. I want
to be able to do this without having all the code in the header since
I know what instances I will need. Thanks in advance -- I believe
I am close on this, but just can't quite get it.
// Foo.hh
using namespace::std;
#include <iostream>
template<int> class Foo;
template<int mp>
std:stream & operator<< (std:stream&, const Foo<mp> &);
template<int mp>
class Foo {
public:
char x[mp];
Foo();
friend std:stream & operator<< <>(std:stream&,const Foo<mp> &);
};
// end of Foo.hh
// Foo.cc
#include "Foo.hh"
template<int mp>
Foo<mp>::Foo()
{
int i;
for (i=0;i<mp;i++);
x = i;
}
template<int mp>
std:stream & operator<< (std:stream&, const Foo<mp> &)
{
cout << "Nothing" << endl;
}
template class Foo<2>;
// end of Foo.cc
If I try to link this code when trying to use cout with an instance of
Foo<2>:
/tmp/test/Main.cc:8: undefined reference to `std::basic_ostream<char,
std::char_traits<char> >& operator<< <(int)2>(std::basic_ostream<char,
std::char_traits<char> >&, Foo<(int)2> const&)'
And to confirm an nm on the object file Foo.o gives me:
nm --demangle Foo.o
00000054 t global constructors keyed to _ZN3FooILi2EEC1EvFoo.ccAPePOb
00000000 t __static_initialization_and_destruction_0(int, int)
00000000 W Foo<2>::Foo()
00000000 W Foo<2>::Foo()
U std::ios_base::Init::Init()
U std::ios_base::Init::~Init()
00000000 b std::__ioinit
U __cxa_atexit
U __dso_handle
U __gxx_personality_v0
00000040 t __tcf_0
code I inherited that needs some work. Basically I have a template
that is instantiated in the c++ code and so only the headers are
distributed. That works great, the only problem is that I cant get
the friend ostream inserter operator to work correctly. Basically
it never is instantiated so it never shows up in the library. Here is
a (close to) minimum example demonstrating the problem. I want
to be able to do this without having all the code in the header since
I know what instances I will need. Thanks in advance -- I believe
I am close on this, but just can't quite get it.
// Foo.hh
using namespace::std;
#include <iostream>
template<int> class Foo;
template<int mp>
std:stream & operator<< (std:stream&, const Foo<mp> &);
template<int mp>
class Foo {
public:
char x[mp];
Foo();
friend std:stream & operator<< <>(std:stream&,const Foo<mp> &);
};
// end of Foo.hh
// Foo.cc
#include "Foo.hh"
template<int mp>
Foo<mp>::Foo()
{
int i;
for (i=0;i<mp;i++);
x = i;
}
template<int mp>
std:stream & operator<< (std:stream&, const Foo<mp> &)
{
cout << "Nothing" << endl;
}
template class Foo<2>;
// end of Foo.cc
If I try to link this code when trying to use cout with an instance of
Foo<2>:
/tmp/test/Main.cc:8: undefined reference to `std::basic_ostream<char,
std::char_traits<char> >& operator<< <(int)2>(std::basic_ostream<char,
std::char_traits<char> >&, Foo<(int)2> const&)'
And to confirm an nm on the object file Foo.o gives me:
nm --demangle Foo.o
00000054 t global constructors keyed to _ZN3FooILi2EEC1EvFoo.ccAPePOb
00000000 t __static_initialization_and_destruction_0(int, int)
00000000 W Foo<2>::Foo()
00000000 W Foo<2>::Foo()
U std::ios_base::Init::Init()
U std::ios_base::Init::~Init()
00000000 b std::__ioinit
U __cxa_atexit
U __dso_handle
U __gxx_personality_v0
00000040 t __tcf_0