18

我有一些code为msvc或替代解决方案模拟__typeof__的最佳方法?

#define DEBUG_PRINT(x,...) \ 
    do \ 
    {\ 
     _Pragma("GCC diagnostic push") \ 
     _Pragma("GCC diagnostic ignored \"-Wunused-value\"") \ 
     __typeof__((0,x)) _x = x; \ 
     _Pragma("GCC diagnostic pop") \ 
     DEBUG_PRINT_PTR((#x), &_x, __VA_ARGS__);\ 
    } while(0) 


//The repetition of debug_print_printf_specifier is to avoid repetition for custom types. 
#define DEBUG_PRINT_PTR(xstr, xp,...) \ 
_Generic((*xp), \ 
const char *: debug_print_printf_specifier(xstr, (void *)xp, TYPE_PTR_TO_PRINTF_SPECIFIER(xp), __FILE__, __LINE__, _my_func__, debug_print_options_apply_group_options(&((debug_print_options){__VA_ARGS__}))),\ 
char *: debug_print_printf_specifier(xstr, (void *)xp, TYPE_PTR_TO_PRINTF_SPECIFIER(xp), __FILE__, __LINE__, _my_func__, debug_print_options_apply_group_options(&((debug_print_options){__VA_ARGS__}))),\ 
int: debug_print_printf_specifier(xstr, (void *)xp, TYPE_PTR_TO_PRINTF_SPECIFIER(xp), __FILE__, __LINE__, _my_func__, debug_print_options_apply_group_options(&((debug_print_options){__VA_ARGS__}))),\ 
float: debug_print_printf_specifier(xstr, (void *)xp, TYPE_PTR_TO_PRINTF_SPECIFIER(xp), __FILE__, __LINE__, _my_func__, debug_print_options_apply_group_options(&((debug_print_options){__VA_ARGS__}))),\ 
double: debug_print_printf_specifier(xstr, (void *)xp, TYPE_PTR_TO_PRINTF_SPECIFIER(xp), __FILE__, __LINE__, _my_func__, debug_print_options_apply_group_options(&((debug_print_options){__VA_ARGS__}))),\ 
char: debug_print_printf_specifier(xstr, (void *)xp, TYPE_PTR_TO_PRINTF_SPECIFIER(xp), __FILE__, __LINE__, _my_func__, debug_print_options_apply_group_options(&((debug_print_options){__VA_ARGS__}))),\ 
int16_t: debug_print_printf_specifier(xstr, (void *)xp, TYPE_PTR_TO_PRINTF_SPECIFIER(xp), __FILE__, __LINE__, _my_func__, debug_print_options_apply_group_options(&((debug_print_options){__VA_ARGS__}))),\ 
uint16_t: debug_print_printf_specifier(xstr, (void *)xp, TYPE_PTR_TO_PRINTF_SPECIFIER(xp), __FILE__, __LINE__, _my_func__, debug_print_options_apply_group_options(&((debug_print_options){__VA_ARGS__}))),\ 
uint32_t: debug_print_printf_specifier(xstr, (void *)xp, TYPE_PTR_TO_PRINTF_SPECIFIER(xp), __FILE__, __LINE__, _my_func__, debug_print_options_apply_group_options(&((debug_print_options){__VA_ARGS__}))),\ 
int64_t: debug_print_printf_specifier(xstr, (void *)xp, TYPE_PTR_TO_PRINTF_SPECIFIER(xp), __FILE__, __LINE__, _my_func__, debug_print_options_apply_group_options(&((debug_print_options){__VA_ARGS__}))),\ 
uint64_t: debug_print_printf_specifier(xstr, (void *)xp, TYPE_PTR_TO_PRINTF_SPECIFIER(xp), __FILE__, __LINE__, _my_func__, debug_print_options_apply_group_options(&((debug_print_options){__VA_ARGS__}))),\ 
default: DEBUG_PRINT_CUSTOM_TYPE(xstr, xp, __VA_ARGS__)) 

#define DEBUG_PRINT_CUSTOM_TYPE(xstr, xp,...) \ 
debug_print_custom_to_debug_string(xstr, xp, &((dsc_func_ptr){GET_CREATE_DEBUG_STRING_FUNC(xp)}), __FILE__, __LINE__, _my_func__,\ 
debug_print_options_apply_group_options(&((debug_print_options){__VA_ARGS__}))) 



#define GET_CREATE_DEBUG_STRING_FUNC(x) _Generic((x), \ 
debug_print_options *: debug_print_options_to_debug_string, \ 
debug_print_group_options *: debug_print_group_options_to_debug_string, \ 
default: print_not_valid_type_for_debug_print) 

我需要在DEBUG_PRINT一个指针x其可以是一个变量或一个表达式。为了支持表达式,我将它分配给一个临时表达式,然后获取它的地址。我可以效仿__typeof___Generic为有限的一组类型,但然后用户需要在2个地方添加自定义类型的行。有没有其他方法可以做到这一点?我可以只支持最新的Microsoft C编译器。

+0

MSVC10 +具有'decltype'。不确定是否可以从C代码访问。 – 2015-03-31 21:51:48

+3

MSVC不执行'_Generic'或'_Pragma'。如果您有/想要使用Visual Studio,那么使用'clang-cl'或C++进行通用编程。 – cremno 2015-03-31 22:16:26

+0

_Pragma只是为了压制警告,MSVC也支持类似的__pragma(https://msdn.microsoft.com/en-us/library/d9x1s805.aspx) – 2015-04-01 04:46:42

回答

-4
char: debug_print_printf_specifier("x"//z.str, (void *)xp, \ 
TYPE_PTR_TO_PRINTF_SPECIFIER(xp), __FILE__, __LINE__, _my_func__, \ 
debug_print_options_apply_group_options(&((debug_print_options{__VA_ARGS__}))),\ 
z=ptr.x 
//just create a ptr z for x... :D 

这么简单..;)

+5

请包括更详细的解决方案说明并对代码进行格式化。 – 2015-09-16 09:44:26