|
Boost Users : |
From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2008-03-24 16:58:53
Type Lib would help if you would like to distinguish only one type or some
special type characteristic. There are some nice traits like: is_same, or
is_base_of, or is_fundamental, or is_scalar, or is_integral etc.
Good Luck,
Ovanes
On 3/24/08, Robert Dailey <rcdailey_at_[hidden]> wrote:
>
> Thanks, mpl::set is even better, I was going to use mpl::vector, which
> probably wouldn't have worked.
>
> I had a feeling the MPL library would be involved, but I wanted to make
> sure the type_traits library didn't already have something in place for this
> kind of situation. Thanks again!
>
> On Mon, Mar 24, 2008 at 3:32 PM, Ovanes Markarian <om_boost_at_[hidden]>
> wrote:
>
> > Hi!
> >
> > Using mpl in your case is not a lot of work ;) Just use the mpl::set for
> > the set of accepted types and the mpl::has_key meta-function in conjunction
> > with enable_if.
> >
> > //////////// UNTESTED CODE ////////////////////
> >
> > #include <boost/utility/enable_if.hpp>
> > #include <boost/mpl/set.hpp>
> > #include <boost/mpl/has_key.hpp>
> >
> >
> > ....
> >
> > typedef set< char,wchar_t> accepted_types;
> >
> >
> > template<class T>
> > void foo( T t
> > , typename
> > boost::enable_if
> > <
> > typename
> > boost::mpl::has_key<accepted_types, T>::type
> > >::type* reserved=0
> > )
> > {
> > ...
> > }
> >
> >
> > /////////////// END OF UNTESTED CODE ////////////////
> >
> >
> > Good Luck,
> > Ovanes
> >
> >
> >
> >
> >
> > On 3/24/08, Robert Dailey <rcdailey_at_[hidden]> wrote:
> >
> > > Hi,
> > >
> > > I have a simple template function that takes one argument. For
> > > example:
> > >
> > > template< typename t_type >
> > > void foo( t_type param )
> > > {
> > > }
> > >
> > >
> > > I want to restrict the type of t_type to a certain subset of types,
> > > such as 'char' or 'wchar_t'. If the user passes in any type other than those
> > > two types, the compiler should not be able to find an overload for foo()
> > > that matches the argument types. Note that I would also be using
> > > boost::enable_if to test this condition, however I do not see anything in
> > > type_traits or anywhere else to perform such a test.
> > >
> > > I thought of using mpl::vector to create a list of types the function
> > > would take and using enable_if to check if the type is in the container, but
> > > this seems like a lot of work. I want to see if there is a simpler, more
> > > compact solution.
> > >
> > > Thanks.
> > >
> > > _______________________________________________
> > > Boost-users mailing list
> > > Boost-users_at_[hidden]
> > > http://lists.boost.org/mailman/listinfo.cgi/boost-users
> > >
> >
> >
> > _______________________________________________
> > Boost-users mailing list
> > Boost-users_at_[hidden]
> > http://lists.boost.org/mailman/listinfo.cgi/boost-users
> >
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net