473,224 Members | 1,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

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 3714
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Suzanne | last post by:
Hi++, I get a link error when I try to call a template function that is declared in a header file and defined in a non-header file. I do *not* get this link error if I define the template...
11
by: Georg Teichtmeister | last post by:
Hello! We are developing a math - library for realtime applications and want to use some given mathlibraries as base(ipp, MTL, .. ). Our library is a wrapper for those and you should be able to...
4
by: jack | last post by:
Hi there, I have a short question about template function definition. 1) The following files will not compile in M$ VC++ but will have no problem with g++. 2) However, if I uncomment all...
2
by: Hartmut Sbosny | last post by:
Hello NG, I have a question. I have a header file with a function template and a fully specialized version of it, for instance //======== File "templ.hpp" ======== #include <iostream> // the...
9
by: Christof Warlich | last post by:
Hi, consider this: template<typename Tclass X { public: void doSomething(T t); }; int main(void) { X<intx;
8
by: nishit.gupta | last post by:
I was having a problem with template class memer function definition , so i serched the net and find that template class member fuction definition should be in header file else compilation will...
9
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
6
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
5
by: chgans | last post by:
Hi all, I'm having difficulties with some template static member, especially when this member is a template instance, for example: ---- template<typename T> class BaseT { public: static...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.