473,657 Members | 2,825 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3742
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...@com Acast.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 "compilatio n".
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...@com Acast.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 "compilatio n".

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...@com Acast.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...@com Acast.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
11527
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 function directly in the header file, or if the change the function to non-template. For example... *** Why am I getting a link error for template function f() in the code below?
11
17811
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 select the underlying library by template parameter. Therefore we split up our library in two namespaces: The first one defines namespace-global functions which call the baselibrary functions. These are templatefunctions which some
4
3001
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 comments in all three files, then it is ok in both compilers.
2
6189
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 general function template... template <class T>
9
1824
by: Christof Warlich | last post by:
Hi, consider this: template<typename Tclass X { public: void doSomething(T t); }; int main(void) { X<intx;
8
7623
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 be successful but not linking. Can somebody help me in telling the exact reason why compiler cannot find the reference of template class member function definition defined in cpp file?? following problem is not linking (undefined reference of...
9
2055
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
6
391
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
3774
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 void UseMap (const std::string &key, int value)
0
8316
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8833
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8737
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8509
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6174
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2735
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1730
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.