Question about enable_if / disable_if
Suppose I have a template function template<typename T> void f(const std::string&, T); which I want there to be a couple of different variants of. Version 1 - Enabled if and only if boost::is_same<T, bool> Version 2 - Enabled if and only if boost::is_same<T, foo> (foo is a class) Version 3 - Enabled if and only if boost::is_enum<T> Version 4 - Otherwise Is there an easy way to express this? It seems I run into a bunch of overload ambiguity problems. I'm sure I can get around this by adding to every version the negation of all the other conditions, but this seems unnecessarily complicated.
2009/5/23 Zachary Turner <divisortheory@gmail.com>
Suppose I have a template function
template<typename T> void f(const std::string&, T);
which I want there to be a couple of different variants of.
Version 1 - Enabled if and only if boost::is_same<T, bool> Version 2 - Enabled if and only if boost::is_same<T, foo> (foo is a class) Version 3 - Enabled if and only if boost::is_enum<T> Version 4 - Otherwise
Is there an easy way to express this? It seems I run into a bunch of overload ambiguity problems. I'm sure I can get around this by adding to every version the negation of all the other conditions, but this seems unnecessarily complicated.
1, 2 and 3 are disjoint, so you can use them as is. For 4 use negation of 1, 2 and 3. Roman Perepelitsa.
Hi! Why do you need an enable_if if you exactly know the type T? In that case you can use the temmplate specialization. template<> void f(const std::string&, bool) {...} template<> void f(const std::string&, foo) {...} template<typename T> void f(const std::string&, typename enable_if<boost::is_enum<T>, T>::type t) {...} On Sat, May 23, 2009 at 7:19 AM, Zachary Turner <divisortheory@gmail.com>wrote:
Suppose I have a template function
template<typename T> void f(const std::string&, T);
which I want there to be a couple of different variants of.
Version 1 - Enabled if and only if boost::is_same<T, bool> Version 2 - Enabled if and only if boost::is_same<T, foo> (foo is a class) Version 3 - Enabled if and only if boost::is_enum<T> Version 4 - Otherwise
Is there an easy way to express this? It seems I run into a bunch of overload ambiguity problems. I'm sure I can get around this by adding to every version the negation of all the other conditions, but this seems unnecessarily complicated.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Sat, May 23, 2009 at 5:55 AM, Ovanes Markarian <om_boost@keywallet.com> wrote:
Why do you need an enable_if if you exactly know the type T? In that case you can use the temmplate specialization.
template<> void f(const std::string&, bool) {...}
template<> void f(const std::string&, foo) {...}
Why template specialization instead of regular functions? Would that somehow force some types to go to the bool or foo overload via an implicit conversion rather than thru the generic catch all template (version 4)? (I've found "the resolution mechanism for a function call will always prefer a normal c++ function in case of equals. But if the best match is with the template function, it will prefer the templates" that seem to say no, but that's hardly an authoritative source though) Obviously I'm only vaguely aware of the exact function resolution rules in this case, so if anyone can shed some more light on this particular example I'd appreciate. Thanks, --DD
template<typename T> void f(const std::string&, typename enable_if<boost::is_enum<T>, T>::type t) {...}
participants (4)
-
Dominique Devienne
-
Ovanes Markarian
-
Roman Perepelitsa
-
Zachary Turner