Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #13734

moving template implementation to cpp

From "U.Mutlu" <for-usenet@mutluit.com>
Newsgroups comp.lang.c++
Subject moving template implementation to cpp
Date 2012-02-04 06:32 +0100
Organization mutluit.com
Message-ID <jgiftc$smk$1@speranza.aioe.org> (permalink)

Show all headers | View raw


This is about a template linking problem:

Below we have a struct TS1, a template class TC1 and a non-template class TC2.
TC2::f() calls TC1<TS1>::f(), it compiles, but linker brings error.
I don't understand why, normally it should work I think.

Why must one place template code in header file?
What's the reason the following doesn't link?
I would like to hide the iplementation by doing it in the cpp file.
Is there a workaround to get this compiled+linked?


//-------- inc.hpp -------------
struct TS1
{
};

struct TS2
{
};

template<typename T = TS1> class TC1
{
public:
   TC1();
   void f();
};
extern TC1<> gC1;

class TC2
{
public:
   TC2();
   void f();
};


//-------- file1.cpp -----------
#include "inc.hpp"

template<typename T> TC1<T>::TC1()
{
}

template<typename T> void TC1<T>::f()
{
}

TC1<> gC1;

int main(int argc, char* argv[])
{
   TC2 C2;
   C2.f();

   return 0;
}


//-------- file2.cpp -----------
#include "inc.hpp"

TC2::TC2()
{
}

void TC2::f()
{
   gC1.f();
}


## Build g++:   g++ file1.cpp file2.cpp
## Build MSC++: cl  file1.cpp file2.cpp

Back to comp.lang.c++ | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

moving template implementation to cpp "U.Mutlu" <for-usenet@mutluit.com> - 2012-02-04 06:32 +0100
  Re: moving template implementation to cpp LR <lruss@superlink.net> - 2012-02-04 00:51 -0500
    Re: moving template implementation to cpp "U.Mutlu" <for-usenet@mutluit.com> - 2012-02-04 20:11 +0100

csiph-web