Passing multiple parameters as a single macro

Question

The C preprocessor has #which converts any expression written after it into an unprocessed string. For instance:

#define make_string(x) #x

int a , b;
const char my_string[] = make_string( a + b ); //my_string holds "a + b"

Is there a way to do the reverse process?
I mean, to get the raw string and convert it to a sequence of tokens. For example ( ~#- this is the theoretical preprocessor operator that does this):

#define make_tokens(x) ~#x

int a = 0 , b = 1;
int c = make_tokens( "a + b" ); //c holds 1 (The result of a + b addition)

Some context (or "Am I falling for an XY problem?")

I am writing a template metaprogramming library that is largely based on the definition of metafunctions and specializes in their various behaviors (for example, when functions are overloaded). For instance:

//Metafunction declaration:
template<typename T , typename U>
struct better_type;


//Metafunction specialization (overload)
template<>
struct better_type<char,unsigned char>
{
    using result = char;
};

, result. , typename function</*function argumments*/>::result, .

, ++ 11 :

  • : , .

    template<typename T>
    struct function_impl;
    

    () , :

    template<>
    struct function_impl<bool>
    {
        using result = /* something */;
    };
    
  • . . .

    template<typename T>
    using function = typename function_impl<T>::result;
    

, : .

- - , CPP. - :

#define define_function( template_args , function_name , function_args ) \
        template< template_args >                                        \
        struct function_name##_impl;                                     \
                                                                         \
        template< template_args >                                        \
        using function_name = typename function_name##_impl< function_args >::type

:

define_function( typename T , my_function , T );

:

template< typename T >
struct mi_function_impl;

template< typename T >
using mi_function = typename my_function_impl< T >::type;

:

define_function( typename T , typename U , mi_binary_function , T , U );

, typename T , typename U , T , U . , . , :

#define define_function( template_args , function_name , function_args ) \
        template< make_tokens(template_args) >                           \
        struct function_name##_impl;                                     \
                                                                         \
        template< template_args >                                        \
        using function_name = typename function_name##_impl< make_tokens(function_args) >::type

, :

define_function( "typename T , typename U" , mi_binary_function , "T , U" );
+3
1

X/Y :

define_function( (typename T, typename U) , mi_binary_function , (T, U) );
// yields:
template< typename T, typename U > struct mi_binary_function_impl; template< typename T, typename U > using mi_binary_function = typename mi_binary_function_impl < T, U >::type;

:

#define strip_parens(...) __VA_ARGS__

#define define_function( template_args , function_name , function_args ) \
    template< strip_parens template_args >                               \
    struct function_name##_impl;                                         \
                                                                         \
    template< strip_parens template_args >                               \
    using function_name = typename function_name##_impl                  \
                                   < strip_parens function_args >::type  // end

( ):

define_function(foo, (class, int, typename));
// yields:
template< class T0, int T1, typename T2 > struct foo_impl; template< class T0, int T1, typename T2 > using foo = typename foo_impl < T0, T1, T2 > :: result;

:

#define gen_single_param_name(number_plus_2)                    \
    BOOST_PP_CAT(T, BOOST_PP_DEC(BOOST_PP_DEC(number_plus_2)))  // end

#define gen_single_param(s, data, elem) elem gen_single_param_name(s)
#define gen_params_from_seq(seq) BOOST_PP_SEQ_TRANSFORM(gen_single_param, _, seq)

#define gen_single_arg(s, data, elem) gen_single_param_name(s)
#define gen_args_from_seq(seq) BOOST_PP_SEQ_TRANSFORM(gen_single_arg, _, seq)

#define define_function_impl(name, param_seq, arg_seq)                    \
    template< BOOST_PP_SEQ_ENUM(param_seq) >                              \
    struct name ## _impl;                                                 \
                                                                          \
    template< BOOST_PP_SEQ_ENUM(param_seq) >                              \
    using name = typename name ## _impl                                   \
                 < BOOST_PP_SEQ_ENUM(arg_seq) > :: result                 // end

#define define_function_seq(name, param_seq)                    \
    define_function_impl(name, gen_params_from_seq(param_seq),  \
                         gen_args_from_seq(param_seq))          // end

#define define_function(name, param_list) \
    define_function_seq(name, BOOST_PP_VARIADIC_TO_SEQ param_list)
+3

All Articles