473,230 Members | 1,425 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,230 software developers and data experts.

Template function definition in source file

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.

I am confused. What should a good compiler do
in both cases?

If the definition of a template
function should always reside in the header file,
why does it help by adding a "wrapper" function
for a template function, after I put its definition
in the wrong place (source file)?

Thank you for your help!

========================= file driver.cpp

#include <iostream>
#include "func.h"
using namespace std;

int main()
{

cout << tf(TwoToOne(g, 2.3), 1.5) << endl;

// cout << ff(1.5) << endl;

return 0;
}

========================== file func.h

#ifndef FUNC_H
#define FUNC_H
class TwoToOne
{
public:
double (*m_f)(double, double);
double m_op;

TwoToOne(double (*f) (double, double), double op)
{
m_f = f;
m_op = op;
}

double operator()(double x)
{
return m_f(x, m_op);
}
};

double g(double x, double y);
template <class T>
double tf(T f, double x);

// double ff(double x);

#endif

============================ file func.cpp

#include "func.h"

double g(double x, double y)
{
return x*x + y;
}

template <class T>
double tf(T f, double x)
{
return f(x);
}

/*
double ff(double x)
{
return tf(TwoToOne(g, 2.3), x);
}
*/
Jul 22 '05 #1
4 2971

Insead of starting a thread of my own, I chip
a question into this one instead. Hope you don't
mind. Anyway:

The FAQ says that there are three solutions to the
linking problem with template classes and functions.
The first one mentioned is the easiest one: Put the
whole definition into the header file. Now, it warns
that this can lead to code bloat, but as someone
mentioned in another thread just reasently, this is
rarely the case with 'modern' compilers.

But say that it doesn't lead to coad bloat on the
compiler you use, are there still reasons not to
just put the complete definition in a header file,
and just leave out a source file for your template
class alltogether?

And wouldn't that sort of simplify adding that file
to other projects, since you would only have to add
the header file, not having to bother with adding the
source file to your project/make file, or having to
create a library?

Did I read somewhere that putting the complete code into
the header files was commonly used in Boost...?

I hope I didn't go OT from the OP now...

--
Fred H

void FredH::Contact() {
TextToSpeach.say("frode at age dee dee dot en oh");
}
Jul 22 '05 #2
"Fred H" <se****@nospam.com> wrote...
[...] are there still reasons not to
just put the complete definition in a header file,
and just leave out a source file for your template
class alltogether?
I can think of only one reason: if you want to hide
the implementation of the function from the users of
your library.

You cannot omit the class definition, though. The
compiler uses it to determine the size of an object.
And wouldn't that sort of simplify adding that file
to other projects, since you would only have to add
the header file, not having to bother with adding the
source file to your project/make file, or having to
create a library?
Yes.
Did I read somewhere that putting the complete code into
the header files was commonly used in Boost...?


And in the Standard Library implementations I've seen, and
in some other libraries...

V
Jul 22 '05 #3
> Victor Bazarov <v.********@comAcast.net> wrote
(...)


Thanks. That simplefied my life a lot :-)
--
Fred H

void FredH::Contact() {
TextToSpeach.say("frode at age dee dee dot en oh");
}
Jul 22 '05 #4
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<ZXuUb.231961$na.368849@attbi_s04>...
"Fred H" <se****@nospam.com> wrote...


Thank you for your help guys. I should've RTFMed before I asked here.

jack
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kevin Frey | last post by:
Hello, Let's say I have the following: template< class T > class MyClass { void Func( ); }; and I want to declare a specialisation for Func( ) in the presence of
4
by: Thomi Richards | last post by:
Hi, I'm trying to create a simple stack class using C++ and templates. Everything works well and good if I amke the class totally inline. However, as soon as I try to seperate the class into a...
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;
9
by: Markus Kern | last post by:
Hi! I've been messing around with a template class. As usual, I do not want to make the definition of my functions public, but only its declaration. This was the concept of seperating...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
5
by: aryan | last post by:
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...
9
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
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...
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
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, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.