c-preprocessor

Using a previously defined #define in a new #define in C

落爺英雄遲暮 提交于 2019-12-12 11:30:00
问题 Are there any potential issues/dangers in doing something such as #define SAMPLERATE 32 // Sample rate in hertz #define ONE_MINUTE ( SAMPLERATE * 60 ) #define FIVE_MINUTES ( ONE_MINUTE * 5 ) My compiler doesn't issue any errors or warnings. It's very nice because I can change one #define value (SAMPLERATE) and all the others get set to what they need to be with no other changes. I'm just not entirely sure if this is best practice or safe. 回答1: A #define is handled by the pre-processor. The

Avoid repetition in C error handling

纵然是瞬间 提交于 2019-12-12 11:16:29
问题 I often write code which ends up being long sequences something like int error; error = do_something(); if (error) { return error; } error = do_something_else(with, some, args); if (error) { return error; } error = do_something_yet_again(); if (error) { return error; } return 0; I'm searching for a cleaner way to write this that to some extent avoids the repeated identical checks. So far, I've written an ERROR_OR macro, which works something like #define ERROR_OR(origerr, newerr) \ ({ \ int _

Is __COUNTER__ macro portable?

家住魔仙堡 提交于 2019-12-12 11:14:45
问题 I have a piece of code which uses __COUNTER__ macro to generate unique names for variables. Is this code portable ? I know that GCC and MSVS support it. What's about other compilers ? Is the macro defined by standard (as far as I know before C++14 it wasn't). 回答1: It's definitely not standard. It's a compiler extension (GNU C extensions) The common predefined macros are GNU C extensions. and a Microsoft-specific one, Microsoft-Specific Predefined Macros: __ COUNTER __ also supported by clang

How to single out the first parameter sent to a macro taking only a variadic parameter

孤街浪徒 提交于 2019-12-12 11:13:27
问题 I try to get at the first actual parameter sent to a variadic macro. This is what I tried, and which does not work in VS2010: #define FIRST_ARG(N, ...) N #define MY_MACRO(...) decltype(FIRST_ARG(__VA_ARGS__)) When I look at the preprocessor output I see that FIRST_ARG returns the entire argument list sent to MY_MACRO ... On the other hand when I try with: FIRST_ARG(1,2,3) it expands to 1 as intended. This seems to be somehow the inverse of the problem solved by the infamous two level concat

Is there any way to convert decltype to string in a macro?

寵の児 提交于 2019-12-12 10:48:58
问题 Is there any way I can evaluate decltype in a C++ macro? My main motivation is to create a macro that is able to determine the type of this and convert it to a string. If it's not possible to use decltype is there any other way a macro used inside a class declaration can get the type of the class as a string? 回答1: Is there any way I can evaluate decltype in a C++ macro? No, since macros are strictly evaluated before decltype . As far as I know there is no way to get the name of the class as a

Mathematical operations during compiler preprocessing

早过忘川 提交于 2019-12-12 10:48:29
问题 I often have the situation where I need several constants generated at compile time for the use of bit shift and masking operations. e.g. #define blockbits 8 #define blocksize 256 // could be generated from 2^blockbits #define blocksize 0xFF // could be generated from blocksize - 1 I would like all these to be generated from blockbits , however there is no power operation that can be used in the preprocessor that I am aware of. Does anyone know a simple way of generating this sort of thing at

How to check if __PRETTY_FUNCTION__ can be used?

拜拜、爱过 提交于 2019-12-12 10:45:42
问题 ...../PluginLoader.h:34: multiple definition of 'Dummy_Func_For_Generating_FUNCTION_NAME_Macro()' The above error is output for the below code. I have include guards in my file. And everything else compiles fine. EDIT: What I was trying to achieve was to check if __PRETTY_FUNCTION__ was defined, and if it was, use it later in code via FUNCTION_NAME macro (For logging purposes). If __PRETTY_FUNCTION__ is not defined, use next best thing and so on. However, the responses I got made me realize

How to detect the libstdc++ version in Clang?

主宰稳场 提交于 2019-12-12 10:34:45
问题 I would like to write a "portable" C++ library in Clang. "Portable" means that I detect (in C preprocessor) what C++ features are available in the compilation environment and use these features or provide my workarounds. This is similar to what Boost libraries are doing. However, the presence of some features depends not on the language, but on the Standard Library implementation. In particular I am interested in: type traits (which of them are available and with what spelling) if initializer

#define a special operator in c++

拟墨画扇 提交于 2019-12-12 09:45:05
问题 Say that I want to make up a special operator !+ in C++ between two objects. I would like to use !+, on example, because I think it is much more meaningful than any other operator. One basic thing I could do is to find a free, unused operator and make the replacement work with a #define: #define !+ % class myclass { public: int operator %(myclass &c) { return 3; } } So that if I later write something like a!+b with a and b instances of myclass, it would work. Now, is there any way to define

In C++11 what should happen first: raw string expansion or macros?

我的未来我决定 提交于 2019-12-12 09:28:22
问题 This code works in Visual C++ 2013 but not in gcc/clang: #if 0 R"foo( #else int dostuff () { return 23; } // )foo"; #endif dostuff(); Visual C++ removes the if 0 first. Clang expands the R raw string first (and never defining dostuff). Who is right and why? 回答1: [Update: Adrian McCarthy comments below saying MSVC++ 2017 fixes this] GCC and clang are right, VC++ is wrong. 2.2 Phases of translation [lex.phases]: [...] The source file is decomposed into preprocessing tokens (2.5) and sequences