c-preprocessor

Is there a C preprocessor macro to print out a struct?

左心房为你撑大大i 提交于 2019-12-23 02:51:16
问题 As far as I can tell, there's no way to print out a struct value in C . i.e., this doesn't fly: typedef struct { int a; double b; } stype stype a; a.a=3; a.b=3.4; printf("%z", a); instead you have to say: printf("a: %d\n", a.a); printf("b: %f\n", a.b); This seems like a perfect place where you could use a macro to save a vast amount of typing for arbitrary structs. Is the C preprocessor powerful enough to perform this transformation? 回答1: I think that the simplest solution (and maybe the most

How can I programmatically force the compiler to stop the compilation process after it encountered a user created error?

北城余情 提交于 2019-12-23 02:05:45
问题 In my code I am using the #error preprocessor directive in order to create an error. However, it still continues the building process. How can I force the building process programmatically to stop after encountering my error? I am using Microsoft Visual Studio 2015, C++ v140, x86, for Windows 10. 回答1: From MSDN The #error directive emits a user-specified error message at compile time and then terminates the compilation. (Emphasis Added) If compilation is continuing after the #error directive,

How can I programmatically force the compiler to stop the compilation process after it encountered a user created error?

限于喜欢 提交于 2019-12-23 02:05:15
问题 In my code I am using the #error preprocessor directive in order to create an error. However, it still continues the building process. How can I force the building process programmatically to stop after encountering my error? I am using Microsoft Visual Studio 2015, C++ v140, x86, for Windows 10. 回答1: From MSDN The #error directive emits a user-specified error message at compile time and then terminates the compilation. (Emphasis Added) If compilation is continuing after the #error directive,

Template function with macro - accumulate on vector

a 夏天 提交于 2019-12-22 17:38:12
问题 I want to create a function that get vector<int> run over all his elements and "sum" them according to specific operator I chose . For example , v1 = [3,6,7] so I could calculate by this function - 3+6+7 of 3-6-7 of 3*6*7 etc .. For this I did - #include <iostream> #include <vector> using namespace std; #define OPERATOR(X,Y,OP) X #OP Y template<T> int allVectorWithOperator(vector<int> &myVector, T) { vector<int>::iterator it; vector<int>::iterator oneBeforeFinal; oneBeforeFinal = myVector.end

Checking if a type is a struct or pointer at compile time in C?

為{幸葍}努か 提交于 2019-12-22 13:53:51
问题 NOTE: This is NOT a duplicate of the question linked by Paul T, because I am asking if it is possible to determine if a type is of a certain broader incomplete type/kind at compile time, not if a symbol has been registered at compile time. This seems like a fundamental misunderstanding of the question. I am writing a library in C that deals with pseudo-generic functions which take a type as an argument through a macro wrapper. To spare the details (because they are rather complicated) there

#if TRUE vs #if YES vs #if 1 are different in Objective-C?

本小妞迷上赌 提交于 2019-12-22 11:13:20
问题 In Xcode 6.1.1 (Obj-C) #if 1 NSLog(@"print 1"); #endif #if TRUE NSLog(@"print TRUE"); #endif #if YES NSLog(@"print YES"); #endif And the result: print 1 print TRUE Can explain to me the result ? Why #if TRUE vs #if YES vs #if 1 are different? 回答1: Mhm... I`m not really an objective-c person, I personally like c++ better, but let me try to answer you anyways. if 1 The compiler, in the end of it, like pretty much everything in computers, runs on zeros and ones. When you write an "if 1"

C++ Compare template type during compile time

孤者浪人 提交于 2019-12-22 10:51:03
问题 I have a template class. Since the templates are processed during compile time, is it possible to compare the template parameter during compile time and use the preprocessor to add specific code? Something like this: template<class T> class MyClass { public: void do() { #if T is equal to vector<int> // add vector<int> specific code #if T is equal to list<double> // add list<double> specific code #else cout << "Unsupported data type" << endl; #endif } }; How can I compare the template types to

Can you nest C preprocessor directives?

白昼怎懂夜的黑 提交于 2019-12-22 09:17:32
问题 For instance, is the following possible: #define definer(x) #define #x? 回答1: Though your syntax is invalid, the answer to your question is technically yes. But it can only be accomplished by nasty tricks that make your code unreadable and unmaintainable. See also: http://www.ioccc.org/years.html#1995_vanschnitz and http://www.ioccc.org/years.html#2004_vik2 回答2: No, you can't do that. The pound ( # ) symbol has a different meaning while in a definition. it means - if this is an argument, make

C++ __COUNTER__ Definition

≡放荡痞女 提交于 2019-12-22 08:59:39
问题 I have two versions of a c++ compiler installed on my computer. One of them recognizes the __COUNTER__ macro and the other does not. After doing some research to make the program compile in both I have yet to come across the Macro definition for __COUNTER__ . Is this some special Macro done by the compiler or can I copy the definition for __COUNTER__ into my source code, if I can copy it what is the code I need. 回答1: __COUNTER__ is a built-in in several common compilers. It is not possible to

C preprocessor library

烂漫一生 提交于 2019-12-22 08:58:24
问题 I have a task of developing source analysis tool for C programs, and I need to pre-process code before the analysis itself. I was wondering what is the best library for this. I need something light-weight and portable. 回答1: Instead of rolling out your own, why not use cpp that's part of the gcc suite: http://gcc.gnu.org/onlinedocs/gcc-4.6.1/cpp/ CPP(1) GNU CPP(1) NAME cpp - The C Preprocessor SYNOPSIS cpp [-Dmacro[=defn]...] [-Umacro] [-Idir...] [-iquotedir...] [-Wwarn...] [-M|-MM] [-MG] [-MF