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 );
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" );
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:
template<typename T , typename U>
struct better_type;
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 = ;
};
. . .
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" );