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


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

moving template implementation to cpp

Started by"U.Mutlu" <for-usenet@mutluit.com>
First post2012-02-04 06:32 +0100
Last post2012-02-04 20:11 +0100
Articles 3 — 2 participants

Back to article view | Back to comp.lang.c++


Contents

  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

#13734 — moving template implementation to cpp

From"U.Mutlu" <for-usenet@mutluit.com>
Date2012-02-04 06:32 +0100
Subjectmoving template implementation to cpp
Message-ID<jgiftc$smk$1@speranza.aioe.org>
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

[toc] | [next] | [standalone]


#13735

FromLR <lruss@superlink.net>
Date2012-02-04 00:51 -0500
Message-ID<4f2cc7a0$0$10048$cc2e38e6@news.uslec.net>
In reply to#13734
U.Mutlu wrote:

> Why must one place template code in header file?


Maybe this will help.

[35.12] Why can't I separate the definition of my templates class from
its declaration and put it inside a .cpp file?

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12

[toc] | [prev] | [next] | [standalone]


#13740

From"U.Mutlu" <for-usenet@mutluit.com>
Date2012-02-04 20:11 +0100
Message-ID<4F2D82E2.4090104@mutluit.com>
In reply to#13735
LR wrote, On 02/04/12 06:51:
> U.Mutlu wrote:
>
>> Why must one place template code in header file?
>
> Maybe this will help.
>
> [35.12] Why can't I separate the definition of my templates class from
> its declaration and put it inside a .cpp file?
>
> http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12

Thanks.
I now found the following IMHO acceptable solution to the/my problem:

//----- file: inc.hpp ------------------------------------------------
/* Moving template implementation out of header file.

    Hiding implementation details by moving it into the cpp file.
    The method below (see esp. file1.cpp) should be put into the
    C++ FAQ under 35.12 and 35.13.
    This method as shown here works for the defaulting typename,
    but one can add more types in a similar fashion as shown in file1.cpp.

    Author: U.Mutlu (www.mutluit.com)

    Build g++:    g++ file1.cpp file2.cpp main.cpp
    Build MSC++:  cl  file1.cpp file2.cpp main.cpp
*/

struct TS1
{
};

struct TS2
{
};

// implementation in file1.cpp
template<typename T = TS1> class TC1
{
public:
   TC1();
   void f();
};
extern TC1<> gC1;


// implementation in file2.cpp
class TC2
{
public:
   TC2();
   void f();   // inside it makes call to gC1.f()  (just for testing)
};


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

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

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

// SOLUTION OF THE (LINK-) PROBLEM: specialize (instantiate) it
// for the desired type (or types, by repeating it for each type):
template<> TC1<TS1>::TC1()
{
}

template<> void TC1<TS1>::f()
{
}


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

TC2::TC2()
{
}

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


//----- file: main.cpp -----------------------------------------------
#include "inc.hpp"

// test application:

TC1<> gC1;

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

   return 0;
}

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web