tags:

views:

1081

answers:

6

The only portable way of using templates at the moment is to implement them in header files by using inline functions.

What does this sentence mean?

+3  A: 

Although Standard C++ has no such requirement, some compilers require that all function and class templates need to be made available in every translation unit they are used in. In effect, for those compilers, the bodies of template functions must be made available in a header file. To repeat: that means those compilers won't allow them to be defined in non-header files such as .cpp files

There is an export keyword which is supposed to migitate this problem, but it's nowhere close to being portable.

Anton Gogolev
Why can't I implement them in .cpp file with the keyword "inline"?
MainID
You can, and you don't have to put "inline" even. But you'd be able to use them just in that cpp file and nowhere else.
vava
+4  A: 

Templates need to be instantiated by the compiler before actually compiling them into object code. This instantiation can only be achieved if the template arguments are known. Now imagine a scenario where a tempate function is declared in a.h, defined in a.cpp and used in b.cpp. When a.cpp is compiled, it is not neccessarily known that the upcoming compilation b.cpp will require an instance of the template, let alone which specific instance would that be. For more header and source files, the situation can quickly get more complicated.

One can argue that compiers can be made smarter to "look ahead" for all uses of the template, but I'm sure that it wouldn't be difficult to create recursive or otherwise complicated scenarios. AFAIK, compliers don't do such look aheads. As Anton pointed out, some compilers support explicit export decarations of tempate instantiations, but not all compilers support it (yet?).

HTH

David Hanak
"export" is standard, but it's just hard to implement so most of the compiler teams just haven't done yet.
vava
export doesn't eliminate the need for source disclosure, nor does it reduce compile dependencies, while it requires a massive effort from compiler builders. So Herb Sutter himself asked compiler builders to 'forget about' export. As the time investment needed would be better spend elsewhere...
Pieter
So I don't think export isn't implemented 'yet'. It'll probably never get done by anyone else than EDG after the others saw how long it took, and how little was gained
Pieter
Wow. Thanks for the insight, Pieter!
David Hanak
If that interests you, the paper is called "Why we can't afford export", it's listed on his blog (http://www.gotw.ca/publications) but no pdf there (a quick google should turn it up though)
Pieter
+15  A: 

Because when instantiating a template, the compiler creates a new class with the template parameter. For example:

template<typename T>
struct Foo
{
    T bar;
    void doSomething(T param) { // do stuff using T }
};

// somewhere in a .cpp
Foo<int> f;

When reading this line, the compiler will create a new class (let's call it FooInt), which is equivalent to this one:

struct FooInt
{
    int bar;
    void doSomething(int param) { // do stuff using int }
}

Consequently, the compiler needs to have access to the implementation of the methods, to instantiate them with the template parameter (in this case int). If these implementation was not in the header, they wouldn't be accessible, and therefore the compiler wouldn't be able to instantiate the template.

A common solution to this is to write the template declaration in a header file, then implement the class in an implementation file (for example .tpp), and include this implementation file at the end of the header.

// Foo.h
template <typename T>
struct Foo
{
    void doSomething(T param);
};

#include "Foo.tpp"

// Foo.tpp
template <typename T>
void Foo<T>::doSomething(T param)
{
    //implementation
}

This way, implementation is still separated from declaration, but is accessible to the compiler. Another solution is to keep the implementation in a .cpp, and explicitly instantiate all the template instances you'll need, in the header:

// Foo.h
template <typename T> struct Foo { ... };

template class Foo<int>;
template class Foo<float>;
// You will only be able to use Foo with int of float

If my explanation isn't clear enough, you can have a look at the C++ FaqLite on this subject.

Luc Touraille
A: 

It means that the most portable way to define method implementations of template classes is to define them inside the template class definition.

template < typename ... >
class MyClass
{

    int myMethod()
    {
       // Not just declaration. Add method implementation here
    }
};
Benoît
+2  A: 

Actually, the C++ standard defines the 'export' keyword that would make it possible to simply declare templates in a header file and implement them elsewhere.

Unfortunately, none of the popular compilers implements this keyword. The only one I know about is the frontend written by the Edison Design Group, which is used by the Comeau C++ compiler. All others are stuck with having to write templates in header files, because the compiler needs the definition of the code for proper instantiation (as others pointed out already).

DevSolar
+1  A: 

Plenty correct answers here, but I wanted to add this (for completeness):

If you, at the bottom of the implementation cpp file, do explicit instantiation of all the types the template will be used with, the linker will be able to find them as usual.

MaHuJa