
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.

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
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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

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
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
#include #include ....
typedef set< char,wchar_t> accepted_types;
template<class T> void foo( T t , typename boost::enable_if < typename boost::mpl::has_key
::type >::type* reserved=0 ) { ... } /////////////// END OF UNTESTED CODE ////////////////
Good Luck, Ovanes
On 3/24/08, Robert Dailey
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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

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
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
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
#include #include ....
typedef set< char,wchar_t> accepted_types;
template<class T> void foo( T t , typename boost::enable_if < typename boost::mpl::has_key
::type >::type* reserved=0 ) { ... } /////////////// END OF UNTESTED CODE ////////////////
Good Luck, Ovanes
On 3/24/08, Robert Dailey
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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Ovanes Markarian
Type Lib would help if you would like to distinguish only one type or some
Good Luck,Ovanes
On 3/24/08, Robert Dailey
wrote: Thanks, mpl::set is even better, I was going to use mpl::vector, which
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. 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
wrote:
If you only have 2 'valid' types then it's easier to use mpl::or_ instead of
mpl::set.
#include
::type * = 0 ) { }
HTH, Roman Perepelitsa.

On Tue, Mar 25, 2008 at 4:38 AM, Roman Perepelitsa < roman.perepelitsa@gmail.com> wrote:
Ovanes Markarian
writes: Type Lib would help if you would like to distinguish only one type or
Good Luck,Ovanes
On 3/24/08, Robert Dailey
wrote: Thanks, mpl::set is even better, I was going to use mpl::vector, which 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. 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
wrote: If you only have 2 'valid' types then it's easier to use mpl::or_ instead of mpl::set.
#include
#include #include using namespace boost; using namespace boost::mpl;
template<class T> void foo ( T, typename enable_if < or_
, is_same > ::type * = 0 ) { }
HTH, Roman Perepelitsa.
Another great idea :)
participants (3)
-
Ovanes Markarian
-
Robert Dailey
-
Roman Perepelitsa