Jump to content

  • Log In with Google      Sign In   
  • Create Account


Best Practices With Templates


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 James Leighe   Members   -  Reputation: 222

Like
0Likes
Like

Posted 28 July 2012 - 05:09 PM

(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, 28 July 2012 - 05:09 PM.


Sponsor:

#2 japro   Members   -  Reputation: 883

Like
8Likes
Like

Posted 28 July 2012 - 05:10 PM

You usually don't have to provide an explicit template argument. The compiler will deduce that for you if possible... So there is usually no syntactical overhead with function templates.
template<class T>
bool IsWithin(T number, T min, T max ) { /*...*/ }

int a,b,c;
double d,e,f;
isWithin(a,b,c); // ok
isWithin(d,e,f); // ok
isWithin(a,e,f); // here the compiler will complain since it cant figure out which type to choose for T
isWithin<double>(a,e,f); // ok

To restrict the types a template will "accept" look at boost::enable_if (or std::enable_if if you are using c++11)

Edited by japro, 28 July 2012 - 05:14 PM.


#3 James Leighe   Members   -  Reputation: 222

Like
2Likes
Like

Posted 28 July 2012 - 05:15 PM

Wow, ok that solves that issue lol

#4 wqking   Members   -  Reputation: 756

Like
2Likes
Like

Posted 29 July 2012 - 09:04 PM

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

Or you can use different types for the different parameters.
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.
However, this depends on your requirement if all the parameters must have the same type.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#5 James Leighe   Members   -  Reputation: 222

Like
0Likes
Like

Posted 02 August 2012 - 04:02 PM

Just to be clear class templates will always require the template parameter and cannot guess which one you want correct?

#6 japro   Members   -  Reputation: 883

Like
1Likes
Like

Posted 02 August 2012 - 04:33 PM

Correct. In C++11 you can somewhat emulate something like that though by using a "factory function" and the auto keyword.

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);
//...


#7 wqking   Members   -  Reputation: 756

Like
1Likes
Like

Posted 02 August 2012 - 08:11 PM

Correct. In C++11 you can somewhat emulate something like that though by using a "factory function" and the auto keyword.

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);
//...

You can do it in C++ 03 if you replace "auto" with the real type.
Holder<T> myholder1 = make_holder(3.15);
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.
template <typename T>
void anotherFunc(const T & value);

anotherFunc((make_holder(3.15)).

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS