Sign in to follow this  

Best Practices With Templates

This topic is 1956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

(C++)

So lets say I had a function that checked if a number was within a range:
[source lang="java"]bool IsWithin( int32 number, int32 min, int32 max );[/source]
I also wanted it to work with all my base types, such as int64, uint16, float32 etc.

The best bet would be a template right?
It would kinda not be fun to have to constantly do 'IsWithin< type >( var, var, var );' for such ubiquitous code, but I can understand it if that's just the only way.


However, although 'IsWithin' will work for any type that has the operators '<' and '>' defined and is therefore 'safe' what about a function that takes the square root? How can I keep people from just trying 'Sqrt< someclass >( classinstance );'?

I know that would throw a compiler error, but assuming there was a situation with a function that would not? I would like to be able to qualify the acceptable types that a template can be used on in some cases if possible. Edited by James Leighe

Share this post


Link to post
Share on other sites
[quote name='japro' timestamp='1343517016' post='4964130']
[code]
template<class T>
bool IsWithin(T number, T min, T max ) { /*...*/ }

isWithin(a,e,f); // here the compiler will complain since it cant figure out which type to choose for T
[/code]
[/quote]
Or you can use different types for the different parameters.
[code]
template <typename T, typename U, typename V>
bool IsWithin(T number, U min, V max ) { /*...*/ }
isWithin(a,e,f); // here the compiler will NOT complain any more.
[/code]
However, this depends on your requirement if all the parameters must have the same type.

Share this post


Link to post
Share on other sites
Correct. In C++11 you can somewhat emulate something like that though by using a "factory function" and the auto keyword.

[code]
template<class T>
struct Holder {
Holder(T value) : value(value) { }
T value;
};

template<class T>
Holder<T> make_holder(T value)
{
return Holder<T>(value);
}

//...

auto myholder1 = make_holder(3.15);
auto myholder2 = make_holder(42);
//...
[/code]

Share this post


Link to post
Share on other sites
[quote name='japro' timestamp='1343946829' post='4965663']
Correct. In C++11 you can somewhat emulate something like that though by using a "factory function" and the auto keyword.

[code]
template<class T>
struct Holder {
Holder(T value) : value(value) { }
T value;
};

template<class T>
Holder<T> make_holder(T value)
{
return Holder<T>(value);
}

//...

auto myholder1 = make_holder(3.15);
auto myholder2 = make_holder(42);
//...
[/code]
[/quote]
You can do it in C++ 03 if you replace "auto" with the real type.
[code]
Holder<T> myholder1 = make_holder(3.15);
[/code]
It may be not quite interesting because you have to specify the template parameter explicitly.
But it's quite useful if the function result is passed to another template function.
[code]
template <typename T>
void anotherFunc(const T & value);

anotherFunc((make_holder(3.15)).
[/code]

Share this post


Link to post
Share on other sites

This topic is 1956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this