Template function definition in source file

J

jack

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);
}
*/
 
F

Fred H

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");
}
 
V

Victor Bazarov

Fred H said:
[...] 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
 
F

Fred H

Victor Bazarov said:

Thanks. That simplefied my life a lot :)


--
Fred H

void FredH::Contact() {
TextToSpeach.say("frode at age dee dee dot en oh");
}
 

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

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,422
Latest member
LatashiaZc

Latest Threads

Top