468,549 Members | 2,250 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 2769

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Thomi Richards | last post: by
9 posts views Thread by Christof Warlich | last post: by
9 posts views Thread by Markus Kern | last post: by
3 posts views Thread by Hamilton Woods | last post: by
5 posts views Thread by aryan | last post: by
5 posts views Thread by chgans | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.