c-preprocessor

Visual studio __VA_ARGS__ issue

Deadly 提交于 2019-12-20 15:34:04
问题 I run cl /P test.cpp, the file and result is as following. test.cpp #define FiltedLog( ...) \ if (logDetail) \ MP_LOG(LOG_INFO, __VA_ARGS__); #define MP_LOG(level,fmt,...) \ BOOAT::LOG("MP", level, fmt, ##__VA_ARGS__) #define LOG(tag,level,fmt,...) \ Log::log(tag, level, "%s: " fmt, __PRETTY_FUNCTION__, ##__VA_ARGS__) int main () { FiltedLog ( "abc", 1, 2); } Cl /P test.cpp : #line 1 "test.cpp" int main () { if (logDetail) BOOAT::Log::log("MP", LOG_INFO, "%s: " "abc", 1, 2, __PRETTY_FUNCTION_

Is it reasonable to use enums instead of #defines for compile-time constants in C?

痞子三分冷 提交于 2019-12-20 11:37:15
问题 I'm coming back to some C development after working in C++ for a while. I've gotten it into my head that macros should be avoided when not necessary in favor of making the compiler do more work for you at compile-time. So, for constant values, in C++ I would use static const variables, or C++11 enum classes for the nice scoping. In C, static constants are not really compile-time constants, and enums may (? or may not?) behave slightly differently. So, is it reasonable to prefer using enums

#include directive: relative to where?

北慕城南 提交于 2019-12-20 10:59:41
问题 I have looked in The C++ Programming Language to try to find the answer to this. When I #include "my_dir/my_header.hpp" in a header, where does it look for this file? Is it relative to the header, relative to the source file that included it, or something else? 回答1: Implementation defined. See what is the difference between #include <filename> and #include “filename”. 回答2: It is relative to both the current source file and to any search paths given (-I for gcc). 回答3: It depends on what syntax

#include directive: relative to where?

断了今生、忘了曾经 提交于 2019-12-20 10:59:09
问题 I have looked in The C++ Programming Language to try to find the answer to this. When I #include "my_dir/my_header.hpp" in a header, where does it look for this file? Is it relative to the header, relative to the source file that included it, or something else? 回答1: Implementation defined. See what is the difference between #include <filename> and #include “filename”. 回答2: It is relative to both the current source file and to any search paths given (-I for gcc). 回答3: It depends on what syntax

How can I use the compile time constant __LINE__ in a string?

℡╲_俬逩灬. 提交于 2019-12-20 09:55:13
问题 I can use __LINE__ as a method parameter just fine, but I would like an easy way to use it in a function that uses strings. For instance say I have this: 11 string myTest() 12 { 13 if(!testCondition) 14 return logError("testcondition failed"); 15 } And I want the result of the function to be: "myTest line 14: testcondition failed" How can I write logError? Does it have to be some monstrosity of a macro? 回答1: Why do you even need it as a string? What's wrong with an integer? Here are two ways

Can you #define a comment in C?

本秂侑毒 提交于 2019-12-20 09:52:12
问题 I'm trying to do a debug system but it seems not to work. What I wanted to accomplish is something like this: #ifndef DEBUG #define printd // #else #define printd printf #endif Is there a way to do that? I have lots of debug messages and I won't like to do: if (DEBUG) printf(...) code if (DEBUG) printf(...) ... 回答1: No, you can't. Comments are removed from the code before any processing of preprocessing directives begin. For this reason you can't include comment into a macro. Also, any

How to make preprocessor generate a string for __LINE__ keyword?

不问归期 提交于 2019-12-20 09:48:29
问题 __FILE__ is replaced with "MyFile.cpp" by C++ preprocessor. I want __LINE__ to be replaced with "256" string not with 256 integer. Without using my own written functions like toString(__LINE__); Is that possible? How can I do it? VS 2008 EDIT I'd like to automatically Find and Replace all throw; statements with throw std::runtime_error(std::string("exception at ") + __FILE__ + " "+__LINE__); in my sources. If I use macro or function to convert __LINE__ into a string I'll need to modify each

Why use #if 0 for block commenting out?

断了今生、忘了曾经 提交于 2019-12-20 09:28:23
问题 Reverse engineering code and I'm kind of appalled at the style, but I wanted to make sure there's no good reason for doing these things.... Is it just me or is this a horrible coding style if ( pwbuf ) sprintf(username,"%s",pwbuf->pw_name); else sprintf(username,"%d",user_id); And why wrap code not intended for compilation in an #if 0 .... #endif Instead of comments? EDIT: So as some explained below, this is due to the possibility to flummox /* */ which I didn't realize. But I still don't

Why use do { } while (0) in macro definition? [duplicate]

假如想象 提交于 2019-12-20 09:02:54
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why are there sometimes meaningless do/while and if/else statements in C/C++ macros? I met code like below: #define ev_io_init(ev,cb,fd,events) \ do { \ ev_init ((ev), (cb)); \ ev_io_set ((ev),(fd),(events)); \ } while (0) I want to know why the author use do { } while (0) here. Is there any difference with this? #define ev_io_init(ev,cb,fd,events) { \ ev_init ((ev), (cb)); \ ev_io_set ((ev),(fd),(events)); \ }

How to resolve int variable before passing to C/C++ Macros? [closed]

旧时模样 提交于 2019-12-20 07:53:54
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed last year . I am trying to execute the following code: #define channel1 10 #define channel(id) channel##id int main(){ int id = 1; cout << channel(id)<<"\n"; return 0; } I get the following error: error: use of undeclared identifier 'channelid' Instead, I want an output to be 10 , as channel(id) should be