c-preprocessor

Macro for static std::string object from literal

戏子无情 提交于 2019-12-04 15:59:23
问题 Suppose I need to call a function foo that takes a const std::string reference from a great number of places in my code: int foo(const std::string&); .. foo("bar"); .. foo("baz"); Calling a function with a string literal like this will create temporary std::string objects, copying the literal each time. Unless I'm mistaken, compilers won't optimize this by creating a static std::string object per literal that can be reused for subsequent calls. I know that g++ has advanced string pool

Temporarily disable gcc warning on redefinition

 ̄綄美尐妖づ 提交于 2019-12-04 15:36:07
问题 I'm trying to make this work (in GCC 4.6) without barking at me. #define FOO "" #define BAR "" #if .... #define FOO "Foo, good sir" #endif #if ... #define BAR "Bar, my lady" #endif .... #define EVERYTHING FOO BAR ... I am going to have a lot of these. So doing it that way instead of: #if ... #define FOO "Foo" #else #define FOO "" #endif Saves a lot of code, and makes it more readable. The warning that I get is: warning: "FOO" redefined [enabled by default] Is there a way to disable this

Including a header file from another directory

戏子无情 提交于 2019-12-04 15:00:16
问题 I have a main directory A with two sub directories B and C . Directory B contains a header file structures.c : #ifndef __STRUCTURES_H #define __STRUCTURES_H typedef struct __stud_ent__ { char name[20]; int roll_num; }stud; #endif Directory C contains main.c code: #include<stdio.h> #include<stdlib.h> #include <structures.h> int main() { stud *value; value = malloc(sizeof(stud)); free (value); printf("working \n"); return 0; } But I get an error: main.c:3:24: error: structures.h: No such file

How to generate a series of random numbers with the C/C++ preprocessor

旧街凉风 提交于 2019-12-04 14:49:45
I would like to generate a series of random numbers with the C preprocessor, and store them in variables for use by my program. OBJECTIVES: I would like to generate a "unique" set of random numbers every time I build my program. A small subset of the variables storing the random numbers will be overwritten with meaningful (i.e. non-random) numbers. I would like it to be impossible for a hacker, by debugging the program or comparing multiple builds, to be able to differentiate the meaningful numbers from the random numbers. I would like the build process to be automated and self-contained. I

Overloading a macro

亡梦爱人 提交于 2019-12-04 14:14:41
I'm trying to overload a macro by the number of parameter. Of course I can't actually overload the macro. I've tried using variadic macros to choose the right macro (using the fact that if __VA_ARGS__ doesn't exist it's supposed to delete the last coma before it - GCC Reference ): #define TEST1() printf("TEST1"); #define TEST2() printf("TEST2"); #define CHOOSER(x, y,FUNC,...) FUNC() #define MANIMACRO(...) CHOOSER(,__VA_ARGS__,TEST1,TEST2) int main(void) { MANIMACRO(1); MANIMACRO(); } The idea was that if __VA_ARGS__ exists it should pass 4 arguments to CHOOSER , where the third one should have

Detect ARM NEON availability in the preprocessor?

空扰寡人 提交于 2019-12-04 14:11:12
问题 According to the ARM ARM, __ARM_NEON__ is defined when Neon SIMD instructions are available. I'm having trouble getting GCC to provide it. Neon available on this BananaPi Pro dev board running Debian 8.2: $ cat /proc/cpuinfo | grep neon Features : swp half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt I'm using GCC 4.9: $ gcc --version gcc (Debian 4.9.2-10) 4.9.2 Try GCC and -march=native : $ g++ -march=native -dM -E - </dev/null | grep -i neon #define __ARM_NEON_FP 4 OK, try what

Invalid token at start of a preprocessor expression Xcode [duplicate]

為{幸葍}努か 提交于 2019-12-04 14:08:37
This question already has an answer here : Macro in Objective-C calling isEqualToString: produces error about invalid token (1 answer) Closed 3 years ago . #define A7VERSION() ({[[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."] objectAtIndex:0] intValue];}) #define IS_OS_7 A7VERSION()>=7 The above declaration seems to compile well. But as soon as I add it a .m file I get the following exception "invalid token at start of a preprocessor expression". I am not able to understand where I could be wrong @implementation AppViewController #if IS_OS_7 …. #else …. #endif

Can't use macro define class in C++

浪尽此生 提交于 2019-12-04 13:57:21
I want to generate many sub-class which only has little difference, so I want to use macro to simplify my job. The macro define below: #define DECLARE_SUB_CLASS(sub_class_name, base_class_name, value1) \ class sub_class_name:base_class_name \ { \ public: \ virtual int initialize(const void *); \ virtual int run(const void *); \ virtual void reset(); \ virtual int output(const char*); \ virtual void terminate(); \ private: \ static const char m_szValue=#value1; \ }; I use it like this: DECLARE_SUB_CLASS(RTCount13, RTCountBase, 13); when I compiling with VC2005, it say error C2065: 'RTCount13' :

Macro overloading

坚强是说给别人听的谎言 提交于 2019-12-04 13:39:52
Is it possible to define something like this: #define FOO(x, y) BAR() #define FOO(x, sth, y) BAR(sth) so that this: FOO("daf", sfdas); FOO("fdsfs", something, 5); is translated to this: BAR(); BAR(something); ? Edit: Actually, BAR 's are methods of my class. Sorry for not saying that before (didn't think it was relevant). Answering DyP's question: class Testy { public: void TestFunction(std::string one, std::string two, std::string three) { std::cout << one << two << three; } void AnotherOne(std::string one) { std::cout << one; } void AnotherOne(void) { std::cout << ""; } }; #define PP_NARG(..

Overload C/C++ preprocessor macro on structure of its argument

五迷三道 提交于 2019-12-04 12:16:32
问题 I would like to write a preprocessor macro that does one thing if it's argument is a parenthesized tuple of tokens, like this: MY_MACRO((x, y)) and something else if it's just a single token, like this: MY_MACRO(x) Is that possible? How about distinguishing between the number of space-separated tokens, i.e. between MY_MACRO(x) and MY_MACRO(x y) ? Note that I am not trying to overload based on the number of arguments - it's a unary macro in all cases. EDIT : I am willing to use variadic macros