template function definition in cpp file

A

aryan

Is it not allowed to have template function definition in a .cc file?

Here is the scenario. The tempage function declaration is in a header
file and the definition in a .cc file. The function is called in main
which is in another .cc file. With g++ compiler, this fails to
link.However, it compiles if the template function definitioin also
included in header file.

Is there a way with g++ to force template function in .cc file?

Thanks.
 
V

Victor Bazarov

aryan said:
Is it not allowed to have template function definition in a .cc file?

It is allowed. In fact, unless the definition makes its way into
a translation unit (a .cc file), the compiler won't be able to use
the function, according to the ODR. So, even if we put the function
definition in a header file (a .h file), we need to include that
header in a translation unit so the definition gets *translated*.
Here is the scenario. The tempage function declaration is in a header
file and the definition in a .cc file. The function is called in main
which is in another .cc file. With g++ compiler, this fails to
link.However, it compiles if the template function definitioin also
included in header file.

Is there a way with g++ to force template function in .cc file?

Read the FAQ. Search for "template link error". It is always a good
idea to read the FAQ before posting. All of it.

V
 
M

Michael.Boehnisch

Is it not allowed to have template function definition in a .cc file?

Extern templates are proposed for the C++09 standard. Right now
compilers are not required to provide extern template definitions in
order to be C++ conformant. Right now it is common practice to write
template code as inline functions, or to put them in a separate .cc
file and #include it at the end of the header. This works on all C++
compilers I am aware of - your mileage may vary.

I use the following schema for template classes with optional non-
template bases:

classname_base.hpp
Non-template base class definition, if any.
Defines members that are independent from the
template parameters.

classname_base.cpp
Non-template base class implementation, if any.
#includes classname_base.hpp near file start.

classname.hpp
Template class definition, optionally derived
from classname_base.
#includes classname_base.hpp near file start,
#includes classname_impl.cpp at end of file.

classname_impl.cpp
Template class implementation.
#includes none of the above.

classname_impl.cpp is *not* to be compiled as a translation unit.

For gcc specifics, see gcc.info, section 6.6: "Where's the Template?".

best,

Michael
 
P

Pete Becker

Extern templates are proposed for the C++09 standard. Right now
compilers are not required to provide extern template definitions in
order to be C++ conformant.

That's been a requirment since the original C++ standard. For C++0x
there's a small addition that uses "extern" to say that a template
specialization has been instantiated somewhere else, so the compiler
doesn't need to do it here. But that's unrelated to where you define
the templates themselves.
 
H

Helmut Zeisel

That's been a requirment since the original C++ standard. For C++0x
there's a small addition that uses "extern" to say that a template
specialization has been instantiated somewhere else, so the compiler
doesn't need to do it here. But that's unrelated to where you define
the templates themselves.

Just to understand:

Is the following code required to compile?

template<typename T> void myF(T& t);
int main(int argc, const char* argv[])
{
int i;
myF(i);
return 0;
}


It does compile on VC++ 2005;
of course I will get an error at link time:

unresolved external symbol "void myF<int>(int &)"

If I create an object file myf_instantiations.obj with an explicit
instantiation of "void myF<int>(int &)" and add it to the linker
input, linking will succeed without actual access to the internal
definition of myF.

Is this required/allowed by the standard?

Helmut
 
H

Helmut Zeisel

Sure. The point here is, you need to provide the definition of
the function, and where it comes from is unknown *at the time of
compiling a call to it*.
Yes

What you can do is provide another
module with the definition of the function *template* and, along
with that, an _explicit_ instantiation for myF<int>.

Do *I* have to provide is or is it enough when someone else does this
and just
provides a compiled version without giving access to the source of
myF said:
... and puts
an instruction for the linker to look for it in all other places
where the linker usually looks for stuff. The linker does, but
cannot find the definition of myF<int>.

Same question again: does the linker need the definition/source of
myF said:
Now, technically speaking, the code you presented here is NOT
a complete C++ program because it does not contain a *definition*
of myF<int> (or anything from which such definition can be made).
In that sense, *linking* is an integral part of what the Standard
calls "compilation".

As I understand this means that I have to provide the definition/
source of myF<T>.
VC++ 2005 was able to link when just the a compiled version of
myF<int> was available.

Helmut
 
H

Helmut Zeisel

The Standard requires the _definition_
to *exist*. And since the Standard only talks of the source form,
then yes, the source form *has to exists somewhere*. Do you have
to give it to the compiler/linker every time you build your 'main'
program in the *source form*? Usually no.

Up to now I had the impression that if I deliver a template interface
to a customer I also have to deliver the template definition. As I now
understand this is not true. It is possible just to deliver some
instantiations without source. Of course, however, it is not possible
for the customer to create additional instantiations.

Helmut
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top