Problem with type traits
Hi,
I'm trying to create my own struct that is compatible with boost::mpl::or_
as well as boost::enable_if.
I find that using boost::mpl::or_ requires that my structure have a 'type'
member, and enable_if looks for 'value'. Must I define both? What is the
proper way to handle this? Below is my structure:
template< typename t_type >
struct is_sink
{
typedef typename boost::mpl::or_<
vfx::io::is_device
::type& operator>> ( t_source& source, t_data& data ) { source.Read( reinterpret_castboost::uint8_t*( &data ), sizeof( t_data ) ); return source; }
on Sun Jul 20 2008, "Robert Dailey"
Hi,
I'm trying to create my own struct that is compatible with boost::mpl::or_ as well as boost::enable_if.
I find that using boost::mpl::or_ requires that my structure have a 'type' member, and enable_if looks for 'value'.
Both metafunctions are defined so they will work with an MPL integral constant http://www.boost.org/doc/libs/1_35_0/libs/mpl/doc/refmanual/integral-constan...
Must I define both?
Usually the best way to do this is to derive your struct from mpl::true_ or mpl::false_ or some other bool-valued nullary MPL metafunction.
What is the proper way to handle this? Below is my structure:
template< typename t_type > struct is_sink { typedef typename boost::mpl::or_< vfx::io::is_device
, boost::is_same > type; ^ missing "::type" here? Otherwise, the "typename" above is probably illegal
};
provided of course that vfx::io::is_device
The above structure works perfectly fine with boost::mpl::or_, but does not work for boost::enable_if because it is looking for a 'value' member. I feel what it should be looking for is is_sink::type::value. An example of how I use the above structure is below:
template< typename t_source, typename t_data > typename boost::enable_if< boost::mpl::and_
, boost::is_pod >, t_source ::type& operator>> ( t_source& source, t_data& data ) { source.Read( reinterpret_castboost::uint8_t*( &data ), sizeof( t_data ) ); return source; }
I don't see is_sink in there. This might help you: <plug> http://boostpro.com/tmpbook </plug> -- Dave Abrahams BoostPro Computing http://www.boostpro.com
On Sun, Jul 20, 2008 at 11:24 PM, David Abrahams
on Sun Jul 20 2008, "Robert Dailey"
wrote: Hi,
I'm trying to create my own struct that is compatible with boost::mpl::or_ as well as boost::enable_if.
I find that using boost::mpl::or_ requires that my structure have a 'type' member, and enable_if looks for 'value'.
Both metafunctions are defined so they will work with an MPL integral constant
http://www.boost.org/doc/libs/1_35_0/libs/mpl/doc/refmanual/integral-constan...
Must I define both?
Usually the best way to do this is to derive your struct from mpl::true_ or mpl::false_ or some other bool-valued nullary MPL metafunction.
What is the proper way to handle this? Below is my structure:
template< typename t_type > struct is_sink { typedef typename boost::mpl::or_< vfx::io::is_device
, boost::is_same > type; ^ missing "::type" here? Otherwise, the "typename" above is probably illegal };
provided of course that vfx::io::is_device
is a integral-valued nullary metafunction, template <class t_type> struct is_sink : mpl::or_< vfx::io::is_device
, boost::is_same > {}; The above structure works perfectly fine with boost::mpl::or_, but does not work for boost::enable_if because it is looking for a 'value' member. I feel what it should be looking for is is_sink::type::value. An example of how I use the above structure is below:
template< typename t_source, typename t_data > typename boost::enable_if< boost::mpl::and_
, boost::is_pod >, t_source ::type& operator>> ( t_source& source, t_data& data ) { source.Read( reinterpret_castboost::uint8_t*( &data ), sizeof( t_data ) ); return source; }
I don't see is_sink in there.
Sorry, I posted the wrong code example. The is_source in there should be is_sink. I didn't find it very easy to find this answer myself via the boost documentation, though I wish it had been. Thanks for everyone's help. I'll try inheritance and see what I get.
AMDG David Abrahams wrote:
What is the proper way to handle this? Below is my structure:
template< typename t_type > struct is_sink { typedef typename boost::mpl::or_< vfx::io::is_device
, boost::is_same > type; ^ missing "::type" here? Otherwise, the "typename" above is probably illegal
typename is legal because the or_ is qualified. It doesn't matter that boost::mpl is a namespace. (Don't you just love these intuitive rules) In Christ, Steven Watanabe
On Mon, Jul 21, 2008 at 11:29 AM, Steven Watanabe
AMDG
David Abrahams wrote:
What is the proper way to handle this? Below is my structure:
template< typename t_type > struct is_sink { typedef typename boost::mpl::or_< vfx::io::is_device
, boost::is_same type;
^ missing "::type" here? Otherwise, the "typename" above is probably illegal
typename is legal because the or_ is qualified. It doesn't matter that boost::mpl is a namespace. (Don't you just love these intuitive rules)
Mind emphasizing a bit? What do you mean by the or_ being qualified? You mean because I access it through namespace scope? I'm not sure what you mean...
AMDG Robert Dailey wrote:
typename is legal because the or_ is qualified. It doesn't matter that boost::mpl is a namespace. (Don't you just love these intuitive rules)
Mind emphasizing a bit? What do you mean by the or_ being qualified? You mean because I access it through namespace scope?
Yes. Legal: typedef typename boost::mpl::or_<...> type; Not legal: using boost::mpl::or_; typedef typename or_<...> type; In Christ, Steven Watanabe
Robert Dailey wrote:
Hi,
I'm trying to create my own struct that is compatible with boost::mpl::or_ as well as boost::enable_if.
I find that using boost::mpl::or_ requires that my structure have a 'type' member, and enable_if looks for 'value'. Must I define both? What is the proper way to handle this? Below is my structure:
template< typename t_type > struct is_sink { typedef typename boost::mpl::or_< vfx::io::is_device
, boost::is_same > type; }; The above structure works perfectly fine with boost::mpl::or_, but does not work for boost::enable_if because it is looking for a 'value' member. I feel what it should be looking for is is_sink::type::value. An example of how I use the above structure is below:
template< typename t_source, typename t_data > typename boost::enable_if< boost::mpl::and_<*is_source
*, boost::is_pod >, t_source ::type& operator>> ( t_source& source, t_data& data ) { source.Read( reinterpret_castboost::uint8_t*( &data ), sizeof( t_data ) ); return source; } I think the condition has to satisfy this concept: http://www.boost.org/doc/libs/1_35_0/libs/mpl/doc/refmanual/integral-constan...
Anyhow, I think it is a lot easier in general to use inheritance for
metafunctions: i.e. (untested)
template< typename t_type >
struct is_sink:
::boost::mpl::or_<
::vfx::io::is_device
participants (4)
-
David Abrahams
-
John C. Femiani
-
Robert Dailey
-
Steven Watanabe