template<class T> in the h file and cpp file?

S

Simon

Hi,

I am trying to create a round kind of function, (to round float and double
to int).
In the H file I can do the following with not too much trouble...

//
#include <limits>
#include <math.h>

template<class T>
int round( T x )
{
if(x > std::numeric_limits<int>::max()){
return 0;
}

return (int)(x + (x > 0 ? 0.5 : -0.5));
}

//
//

But how can I define it in the h file and then implement it in the cpp file?

//
// myfile.h
template<class T> int round( T x );

//
// myfile.cpp
template<class T>
int round( T x )
{
// ..
}

And how's my round(...) function looking?

Simon
 
V

Victor Bazarov

Simon said:
I am trying to create a round kind of function, (to round float and
double to int).
In the H file I can do the following with not too much trouble...

//
#include <limits>
#include <math.h>

template<class T>
int round( T x )
{
if(x > std::numeric_limits<int>::max()){
return 0;
}

return (int)(x + (x > 0 ? 0.5 : -0.5));
}

//
//

But how can I define it in the h file and then implement it in the
cpp file?
[..]

This is covered in the FAQ. Please see the section on templates.
And how's my round(...) function looking?

OK. You know, if it does what you need it to do, why do you care how
it looks?

V
 
M

mlimber

Simon said:
Hi,

I am trying to create a round kind of function, (to round float and double
to int).

Consider using Boost's Numeric Conversion Library
(http://boost.org/libs/numeric/conversion/) instead of rolling your
own.
In the H file I can do the following with not too much trouble...

//
#include <limits>
#include <math.h>

You don't use math.h, but if you did, you should probably prefer
template<class T>
int round( T x )
{
if(x > std::numeric_limits<int>::max()){

What if x said:
return 0;

Is returning an incorrect value better than throwing a range exception?
Maybe depending on your circumstances, but I'd probably prefer the
latter.
}

return (int)(x + (x > 0 ? 0.5 : -0.5));
}

//
//

But how can I define it in the h file and then implement it in the cpp file?
[snip]

Use the export keyword if your compiler supports it (most don't; see
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.14).

Cheers! --M
 
E

eastern_strider

I think you can't.

There might be ways to define the functions of a static class in a .cpp
file, but I believe according to the standards both the declaration and
the definition should be in a .h file.
 
M

mlimber

eastern_strider said:
I think you can't.

There might be ways to define the functions of a static class in a .cpp
file, but I believe according to the standards both the declaration and
the definition should be in a .h file.

No. According to the Standard, you can use the export keyword. It's
just that few compilers support (or plan to support) the export
keyword. See the aforementioned FAQ.

Cheers! --M
 

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
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top