Quantcast
Channel: Overloading a function using templates - Stack Overflow
Viewing all articles
Browse latest Browse all 5

Answer by StoryTeller - Unslander Monica for Overloading a function using templates

$
0
0

There are a couple of ways to accomplish this. All involve using the type_traits header. You can static assert on the types in question in the body of the function, for instance.

Or, if you need to consider this function among other overloads, a SFINAE technique can be employed.

template<typename T>auto isFunction(const T &aVariable)   -> std::enable_if_t<std::is_same<T, int>::value || std::is_same<T,anEnum>::value, bool> {}

This will remove the function from an overload set before it's called if the types don't match. But if you don't need this behavior, a static assertion does allow for a more programmer friendly error message.


Viewing all articles
Browse latest Browse all 5

Trending Articles





<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>