[type_traits]: is_boost_optional?
Hi, is there something in Boost.TypeTraits (or somewhere else) that allows me to check if a type is a boost::optional? I'd like to do something like this: template<typename T> typename enable_if<is_boost_optional<T> >, T::value_type>::type foo(const T&); Thanks and regards, Roland
2010/9/1 Roland Bock <rbock@eudoxos.de>
Hi,
is there something in Boost.TypeTraits (or somewhere else) that allows me to check if a type is a boost::optional?
I'd like to do something like this:
template<typename T> typename enable_if<is_boost_optional<T> >, T::value_type>::type foo(const T&);
You can use is_instance_of from lambda library (see boost/lambda/detail/is_instance_of.hpp). It's not part of public interface though, so use it on your own risk. template <class T> struct is_boost_optional : boost::lambda::is_instance_of_1<T, boost::optional> {} Roman Perepelitsa.
On 2010-09-01 14:13, Roman Perepelitsa wrote:
2010/9/1 Roland Bock <rbock@eudoxos.de <mailto:rbock@eudoxos.de>>
Hi,
is there something in Boost.TypeTraits (or somewhere else) that allows me to check if a type is a boost::optional?
I'd like to do something like this:
template<typename T> typename enable_if<is_boost_optional<T> >, T::value_type>::type foo(const T&);
You can use is_instance_of from lambda library (see boost/lambda/detail/is_instance_of.hpp). It's not part of public interface though, so use it on your own risk.
template <class T> struct is_boost_optional : boost::lambda::is_instance_of_1<T, boost::optional> {}
Cool, thanks! Regards, Roland
On 2010-09-01 14:13, Roman Perepelitsa wrote:
2010/9/1 Roland Bock <rbock@eudoxos.de <mailto:rbock@eudoxos.de>>
Hi,
is there something in Boost.TypeTraits (or somewhere else) that allows me to check if a type is a boost::optional?
I'd like to do something like this:
template<typename T> typename enable_if<is_boost_optional<T> >, T::value_type>::type foo(const T&);
You can use is_instance_of from lambda library (see boost/lambda/detail/is_instance_of.hpp). It's not part of public interface though, so use it on your own risk.
template <class T> struct is_boost_optional : boost::lambda::is_instance_of_1<T, boost::optional> {}
Roman Perepelitsa.
This seems to work, too: // ------------------------------------------------------------- template <typename T, typename Enable = void> class is_boost_optional { public: static const bool value = false; }; template <typename T> class is_boost_optional < T, typename boost::enable_if < boost::is_same < boost::optional<typename T::value_type>, T > >::type > { public: static const bool value = true; }; // ------------------------------------------------------------- Regards, Roland
Zitat von Roland Bock <rbock@eudoxos.de>:
Hi,
is there something in Boost.TypeTraits (or somewhere else) that allows me to check if a type is a boost::optional?
I'd like to do something like this:
template<typename T> typename enable_if<is_boost_optional<T> >, T::value_type>::type foo(const T&);
there is nothing in type traits, but you can easily check for a specific (template) type: template<typename T> struct is_optional : mpl::false_{}; template<typename T> struct is_optional<optional<T> > : mpl::true_{}; I don't see why you can't use an overload for boost::optional in the first place though: typename optional<T>::value_type foo(optional<T> const &);
On 2010-09-01 15:30, Stefan Strasser wrote:
Zitat von Roland Bock <rbock@eudoxos.de>:
Hi,
is there something in Boost.TypeTraits (or somewhere else) that allows me to check if a type is a boost::optional?
I'd like to do something like this:
template<typename T> typename enable_if<is_boost_optional<T> >, T::value_type>::type foo(const T&);
there is nothing in type traits, but you can easily check for a specific (template) type:
template<typename T> struct is_optional : mpl::false_{};
template<typename T> struct is_optional<optional<T> > : mpl::true_{};
cerr << is_boost_optional<boost::optional<int> >::value << endl; yields: 0 expected: 1
I don't see why you can't use an overload for boost::optional in the first place though:
typename optional<T>::value_type foo(optional<T> const &);
Hmm. The situation is a bit more complex: I have operands of the following type template<typename A, typename B, typename Value> struct Field { typedef Value value_type; template<typename T> some_sfinae_magic operator==(const T&) const; } Value can be any numeric type, a string or an optional numeric/string. T can be numeric type, string or a Field. some_magic is supposed to check, if T is compatible to Field, either is_convertible<Field::value_type, T> is_convertible<Field::value_type, T::value_type> // with T being a Field is_convertible<Field::value_type::value_type, T::value_type> // with T being a Field and Field::value_type being an optional is_convertible<Field::value_type::value_type, T::value_type::value_type> // with T being a Field, both Fields having optional value_type Since value_type is pretty common, a Field with Value=optional<char> and string would be found compatible because string::value_type=char. That's why I want to check if the Value/T::value_type is an optional. Thanks for asking, I guess I just found a way to clean it up a little while writing the stuff down. Regards, Roland
Zitat von Roland Bock <rbock@eudoxos.de>:
On 2010-09-01 15:30, Stefan Strasser wrote:
Zitat von Roland Bock <rbock@eudoxos.de>:
Hi,
is there something in Boost.TypeTraits (or somewhere else) that allows me to check if a type is a boost::optional?
I'd like to do something like this:
template<typename T> typename enable_if<is_boost_optional<T> >, T::value_type>::type foo(const T&);
there is nothing in type traits, but you can easily check for a specific (template) type:
template<typename T> struct is_optional : mpl::false_{};
template<typename T> struct is_optional<optional<T> > : mpl::true_{};
cerr << is_boost_optional<boost::optional<int> >::value << endl;
yields: 0 expected: 1
On 2010-09-01 16:39, Stefan Strasser wrote:
On 2010-09-01 15:30, Stefan Strasser wrote:
Zitat von Roland Bock <rbock@eudoxos.de>: cerr << is_boost_optional<boost::optional<int> >::value << endl;
yields: 0 expected: 1
Ooops! Sorry, brain dead user (me) error :-( Thanks, works beautifully! Regards, Roland
participants (3)
-
Roland Bock
-
Roman Perepelitsa
-
Stefan Strasser