18

mam jakiś codeNajlepszy sposób na emulację __typeof__ dla msvc lub alternatywnego rozwiązania alternatywnego?

#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) 

muszę wskaźnik do x w DEBUG_PRINT które mogą być zmienną lub wyrażeniem. Aby obsługiwać wyrażenia, przypisuję je do tymczasowego, a następnie przyjmuję adres tego. Mogę emulować __typeof__ z _Generic dla ograniczonego zestawu typów, ale użytkownicy musieliby dodać linie dla niestandardowych typów w 2 miejscach. Czy jest jakiś inny sposób na zrobienie tego? Byłbym w porządku tylko z obsługą najnowszego kompilatora Microsoft C.

+0

MSVC10 + znajduje 'decltype'. Nie wiem, czy jest to dostępne z kodu C. –

+3

MSVC nie implementuje ani '_Generic' ani' _Pragma'. Jeśli masz/chcesz używać Visual Studio, użyj programu 'clang-cl' lub C++ do programowania ogólnego. – cremno

+0

_Pragma służy do tłumienia ostrzeżeń, również MSVC obsługuje podobną do __pragma (https://msdn.microsoft.com/en-us/library/d9x1s805.aspx) –

Odpowiedz

-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 

proste ..;)

+5

Proszę podać bardziej rozbudowany opis rozwiązania i sformatować kod. –

Powiązane problemy