c-preprocessor

How to create an enum dynamically in compiling time for my struct

ε祈祈猫儿з 提交于 2020-04-16 02:32:05
问题 I have this struct below struct foo { char *name; int (*validate)(u8_t *data, size_t size); u8_t value; u8_t changed; foo_id id; }; typedef struct foo foo_t; I wish I to create an array of foo_t in compiling time through defines, like this: int my_validate(u8_t *data, size_t size) {...} FOO_CREATE(my_name, my_validate, 0, 0); FOO_CREATE(my_name2, NULL, 0, 0); and in compiling time the result be: enum { MY_NAME_FOO = 0, MY_NAME2_FOO, FOO_COUNT } foo_id; static foo_t foo[FOO_COUNT] = { { .name

Why can't a string literal be concatenated to __FUNCTION__?

断了今生、忘了曾经 提交于 2020-04-14 07:36:07
问题 Isn't __FUNCTION__ a string literal? I'd always thought it was - along the lines of __FILE__ , but I just discovered I can't adjacently concatenate a string literal to it. If it's not a string literal, what is it defined as? I can't get cscope to resolve it. E.g. #include <iostream> int main( int argc, char* argv[] ) { std::cout << __FILE__ << std::endl; std::cout << __FILE__ "A" << std::endl; std::cout << __FUNCTION__ << std::endl; //std::cout << __FUNCTION__ "A" << std::endl; // Doesn't

Preprocessor equality test, is this standard?

不羁岁月 提交于 2020-03-17 06:57:06
问题 I had envisaged one of these in the project preferences TESTING = HOST TESTING = TARGET TESTING not defined at all My problem is with the latter. It seems that instead of #if TESTING==HOST #error "HOST defined" // add temporarilly for testing porpoises #endif I need to code #ifdef TESTING #if TESTING==HOST #error "HOST defined" // add temporarilly for testing porpoises #endif #endif I am convinced that this is not-standard behaviour, since if TESTING is not defined then it certainly doesn't

Variadic macro expansion for each token

。_饼干妹妹 提交于 2020-03-03 06:30:36
问题 Suppose I have a macro, a simple one that just calls a function foo for different type: #define FOO(type) foo_##type(); In one go, let's say I want to call this thing for multiple different types. Concretely; foo_int(); foo_float(); foo_point2d(); I want to generate above code with a macro called FOO2 . #define FOO2(args...) --fill--here And just to be complete, FOO2(int, float, point2d) should expand into the above small code snippet. Is this possible with macros and how to do a different,

C++ using C code using double underscores in defines and identifiers

我只是一个虾纸丫 提交于 2020-02-29 00:00:47
问题 I understand that in C++ double underscores in identifiers are reserved for the compiler. I have some C code which has characteristics similar to this in the corresponding header files: extern "C" { #define HELLO__THERE 1 int hello__out__there( int ); } I will be using this header in a C++ project, and plan to be doing things in C++ like: if (HELLO__THERE == abc) hello__out__there(foo); Is this acceptable behavior in C++, covered by the standard? 回答1: double underlines in identifiers are

Combining wide string literal with string macro

孤人 提交于 2020-02-24 03:38:06
问题 I have a macro for a character string as follows: #define APPNAME "MyApp" Now I want to construct a wide string using this macro by doing something like: const wchar_t *AppProgID = APPNAME L".Document"; However, this generates a "concatenating mismatched strings" compilation error. Is there a way to convert the APPNAME macro to a wide string literal? 回答1: Did you try #define APPNAME "MyApp" #define WIDEN2(x) L ## x #define WIDEN(x) WIDEN2(x) const wchar_t *AppProgID = WIDEN(APPNAME) L"

Combining wide string literal with string macro

主宰稳场 提交于 2020-02-24 03:37:11
问题 I have a macro for a character string as follows: #define APPNAME "MyApp" Now I want to construct a wide string using this macro by doing something like: const wchar_t *AppProgID = APPNAME L".Document"; However, this generates a "concatenating mismatched strings" compilation error. Is there a way to convert the APPNAME macro to a wide string literal? 回答1: Did you try #define APPNAME "MyApp" #define WIDEN2(x) L ## x #define WIDEN(x) WIDEN2(x) const wchar_t *AppProgID = WIDEN(APPNAME) L"

Using the C preprocessor to generate function declarations

僤鯓⒐⒋嵵緔 提交于 2020-02-22 04:07:36
问题 I have a lot of functions to declare in this format: int foo_100(int, int); int foo_200(int, int); int foo_300(int, int); int foo_400(int, int); typedef int (*foo)(int, int); struct foo foo_library[] = { foo_100, foo_200, foo_300, foo_400 }; Is there a way I can use the C preprocessor to partially automate this task? Ideally, something like this: foo.txt 100 200 300 400 foo.h typedef int (*foo)(int, int); #define DEFINE_FOO(id_) int foo_##id_(int, int); DEFINE_FOO(#include"foo.txt") struct

Using the C preprocessor to generate function declarations

荒凉一梦 提交于 2020-02-22 04:05:21
问题 I have a lot of functions to declare in this format: int foo_100(int, int); int foo_200(int, int); int foo_300(int, int); int foo_400(int, int); typedef int (*foo)(int, int); struct foo foo_library[] = { foo_100, foo_200, foo_300, foo_400 }; Is there a way I can use the C preprocessor to partially automate this task? Ideally, something like this: foo.txt 100 200 300 400 foo.h typedef int (*foo)(int, int); #define DEFINE_FOO(id_) int foo_##id_(int, int); DEFINE_FOO(#include"foo.txt") struct

Defining something to itself in C preprocessor

天涯浪子 提交于 2020-02-14 12:13:31
问题 I ran into these lines: #define bool bool #define false false #define true true I don't think I need to say more than "wtf?", but just to be clear: What is the point of defining something to itself? The lines come from clang stdbool.h 回答1: The C and C++ standards explicitly allow that (and requires that there is no infinite expansion) BTW, function-like recursive (or self-refential) macros are even more useful: #define puts(X) (nblines++,puts(X)) (the inner puts is a call to the standard puts