473,395 Members | 1,730 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,395 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 2983

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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...

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.