468,247 Members | 1,324 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,247 developers. It's quick & easy.

template function definition in cpp file

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.
Mar 25 '08 #1
5 3397
aryan wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 25 '08 #2
On Apr 24, 3:40 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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<T?
... 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<Tor is it enough if it finds a compiled version of myF<int?
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<intwas available.

Helmut
Jun 27 '08 #3
Helmut Zeisel wrote:
On Apr 24, 3:40 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>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<T?
Are you trying to be cute here? You're the one _linking_ the damn
program, aren't you? So, _you_ are going to *provide the definition*
to your linker. In what form is up to you and your linker. If you
can only get it in the form of an object file, and your linker likes
that, power to you!
>
>... 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<Tor is it enough if it finds a compiled version of myF<int?
It's up to your linker, isn't it? The Standard says nothing about
the linking except that it can be a separate step.
>
>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>.
Whatever floats your boat. 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. But that process (what
we know as "separate compilation") is not standardized. It is
totally up to the implementation to _allow_ that.
VC++ 2005 was able to link when just the a compiled version of
myF<intwas available.
Good.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #4
On 24 Apr., 18:04, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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

Jun 27 '08 #5
Helmut Zeisel wrote:
On 24 Apr., 18:04, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>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.
That's a good way to put it. If you want the user of your class to
limit their use to a specific set of template arguments, you provide
the declarations of the functions and a binary library that contains
the definitions (from explicit instantiations or specialisations of
the template) of those functions, without the source code. Of course
nothing prevents the user from trying to provide their own code that
would specialise the template based on their own template argument[s].

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Georg Teichtmeister | last post: by
9 posts views Thread by Christof Warlich | last post: by
6 posts views Thread by Gaijinco | last post: by
5 posts views Thread by chgans | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.