Hi,

I want to use a template function that is used to convert strings to numeric types in a non-templated class. I have:

class Reader
{
...
template <class T>
T ConvertString (const std::string str);

};

Is it possible for me to define the template function in an implementation source file instead of defining it inside the class? I have been trying to do this but I've been getting an undefined reference error in gcc.

Thanks.

Is it possible for me to define the template function in an implementation source file instead of defining it inside the class?

The short answer is no. The long answer is still no, but it's a longer no, so I'll stick with the short answer. :)

Thanks for the response. Out of interest, exactly why is this the case?

Check if your compiler supports export keyword, although note that its being removed from the standard.

Out of interest, exactly why is this the case?

It's just a historical limitation of templates and template instantiation. Standard C++ introduced the export keyword for supporting separate compilation, but it failed miserably had has been voted off the island for the next revision.

I see. Good to know.

...answer is no...

Technically correct is to say that: Yes, but it doesn't make sense.

As the templates are "compiler feature", definition of a template must be in the same compilation unit as the user. So if you do this all users would have to #include the "implementation source file".

Technically correct is to say that: Yes, but it doesn't make sense.

If it didn't make sense, a separate compilation model (ie. export) wouldn't have been introduced in the first place; separate compilation is still a goal, to the best of my knowledge. In my opinion, export failed not because it didn't make sense, but because it was too difficult to implement (both in complexity and lack of sufficient specification).

So technically correct is to say:

  • No
  • No, but you can fake it somewhat by including the definition file
  • Yes, if your compiler implements the now defunct export, but only if export truly does what you want it to do

As the templates are "compiler feature", definition of a template must be in the same compilation unit as the user.

You're basing your argument on current implementations of the feature, not the spirit of the feature. There's nothing about templates that inherently forces the inclusion model, that was simply a trade-off accepted by compilers for the sake of simplicity.

You're basing your argument on current implementations of the feature, not the spirit of the feature.

I prefer a technical argument to a spirited one. :)

A better argument for your side would be: Refer to "extern templates" in "Explicit instantiation" section in "Templates" chapter in the C++0x, where the future-spirit is described.

For the record by this "but it doesn't make sense." I meant: "It doesn't make sense to define the templates in a .cpp and then #include it in client code"

Anyway, let me not hijack the thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.