c-preprocessor

What's the safest way to define short function name aliases in C++?

独自空忆成欢 提交于 2019-12-08 17:28:15
问题 Suppose I have a class Utility in a file utility.h : class Utility { public: static double longDescriptiveName(double x) { return x + 42; } }; And then I find that I use the function longDescriptiveName(...) a LOT. So like an irresponsible C++ programmer that I am when I've had too much coffee, I create a new file utilitymacros.h and add the following there: #define ldn Utility::longDescriptiveName Now I include "utilitymacros.h" in any *.cpp where I use ldn(...) and my heart is filled with

Do any C or C++ compilers optimize within define macros?

被刻印的时光 ゝ 提交于 2019-12-08 16:18:20
问题 Let's say I have the following in C or C++: #include <math.h> #define ROWS 15 #define COLS 16 #define COEFF 0.15 #define NODES (ROWS*COLS) #define A_CONSTANT (COEFF*(sqrt(NODES))) Then, I go and use NODES and A_CONSTANT somewhere deep within many nested loops (i.e. used many times). Clearly, both have numeric values that can be ascertained at compile-time, but do compilers actually do it? At run-time, will the CPU have to evaluate 15*16 every time it sees NODES , or will the compiler

Combining two #defined symbols in C++ preprocessor

谁说胖子不能爱 提交于 2019-12-08 15:58:43
问题 I want to do: #define VERSION XY123 #define PRODUCT MyApplication_VERSION so that PRODUCT is actually MyApplication_XY123. I have tried playing with the merge operator ## but with limited success... #define VERSION XY123 #define PRODUCT MyApplication_##VERSION => MyApplication_VERSION #define VERSION XY123 #define PRODUCT MyApplication_##(VERSION) => MyApplication_(XY123) - close but not quite Is what I want possible? 回答1: Token pasting works with arguments to macros. So try this #define

sizeof() is not executed by preprocessor

一笑奈何 提交于 2019-12-08 15:55:27
问题 #if sizeof(int) != 4 /* do something */ Using sizeof inside #if doesn't work while inside #define it works, why? #define size(x) sizeof(x)/sizeof(x[0]) /*works*/ 回答1: Nothing is evil - everything can be misused, or in your case misunderstood. The sizeof operator is a compiler feature, but compiler features are not available to the preprocessor (which runs before the compiler gets involved), and so cannot be used in #if preprocessor directives. However, when you say: #define size(x) sizeof(x)

#if 0 as a define

送分小仙女□ 提交于 2019-12-08 15:11:48
问题 I need a way to define a FLAGS_IF macro (or equivalent) such that FLAGS_IF(expression) <block_of_code> FLAGS_ENDIF when compiling in debug (e.g. with a specific compiler switch) compiles to if (MyFunction(expression)) { <block_of_code> } whereas in release does not result in any instruction, just as it was like this #if 0 <block_of_code> #endif In my ignorance on the matter of C /C++ preprocessors I can't think of any naive way (since #define FLAGS_IF(x) #if 0 does not even compile) of doing

Why do major compilers use typedef for stdint.h but use #define for stdbool.h?

混江龙づ霸主 提交于 2019-12-08 15:11:22
问题 I just noticed that gcc and clang both appear to use typedefs for stdint.h but #define for stdbool.h. example: clang's stdint.h #ifdef __INT8_TYPE__ #ifndef __int8_t_defined /* glibc sys/types.h also defines int8_t*/ typedef __INT8_TYPE__ int8_t; #endif /* __int8_t_defined */ typedef __UINT8_TYPE__ uint8_t; # define __int_least8_t int8_t # define __uint_least8_t uint8_t #endif /* __INT8_TYPE__ */ clang's stdbool.h #ifndef __cplusplus #define bool _Bool #define true 1 #define false 0 #elif

What is an appropriate use scenario of #define in C++? [closed]

浪尽此生 提交于 2019-12-08 14:39:43
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . I know the basic rules, use inline , enum and const instead of #define , that is not what I'm after with this question. What I want to know is what is considered an acceptable scenario in which you would use a #define macro, and how, in C++. Please do not post question or

C preprocessor macro: concatenation (example for Fortan90)

佐手、 提交于 2019-12-08 11:21:08
问题 I am stuck with the following problem: I have the two following correct working macros (expanding to Fortran90 code): #define ld_dx(A) ( (A(ixp1)-A(ix ))/(dx) ) #define rd_dx(A) ( (A(ix )-A(ixm1))/(dx) ) (Note: these macros depend on the following additional macros: #define ix 2:nx-1 #define ixp1 1+ix+1 #define ixm1 -1+ix-1 And they depend also the declarations: integer, parameter :: nx = 100, dx = 1 ) In my code I can use these macros by a call as for example X = X + ld_dx(Y) or: X = X + rd

Can I #define a constant solutionwide within c# code without project settings?

十年热恋 提交于 2019-12-08 10:39:59
问题 I know this was aksed and answered a a couple of times e.g. Solution-wide #define, Is There anyway to #define Constant on a Solution Basis? and How to define a constant globally in C# (like DEBUG). But in my case I can not use any of the suggested methods: I'm writing on different "modules" (or plugins if you want so) for UnityProjects (kind of a package providing a certain functionality). The idea is that a developer can load a certain "module" to use in his project by importing a

Can anybody please explain the behavour of C preprocessor in following examples?

允我心安 提交于 2019-12-08 08:39:25
问题 I am implementing a C macro preprocessor (C99)... I am surprised by the following behaviour.... Ex1: #define PASTE(x) X_##x #define EXPAND(x) PASTE(x) #define TABSIZE 1024 #define BUFSIZE TABSIZE PASTE(BUFSIZE) EXPAND(BUFSIZE) expands to: X_BUFFSIZE X_1024 Ex2: #define EXPAND(s) TO_STRING(s) #define TO_STRING(s) #s #define FOUR 4 TO_STRING(FOUR) EXPAND(FOUR) Expands to: "FOUR" "4" I have gone through the "free" standard of C but I couldn’t find following things... Actually how many passes