preprocessor-directive

sizeof in preprocessor command doesn't compile with error C1017

别来无恙 提交于 2021-02-17 07:23:05
问题 I want to use preprocessor command to control code executive path. Because in this way can save runtime time. #if (sizeof(T)==1 doesn't comple with error: C1017 template<typename T> class String { public: static void showSize() { #if (sizeof(T)==1) cout << "char\n"; #else cout << "wchar_t\n"; #endif } }; inline void test() { String<char>::showSize(); String<wchar_t>::showSize(); } 回答1: The preprocessor runs before the C++ compiler. It knows nothing of C++ types; only preprocessor tokens.

Why is this program produces different result with `YES` and `true`?

拈花ヽ惹草 提交于 2021-02-17 02:06:16
问题 Here is the full program. Can you figure out its console output? #import <Foundation/Foundation.h> #define kEnv YES #if kEnv #define x @"abc" #else #define x @"xyz" #endif #define kVersion true #if kVersion #define y @"abc" #else #define y @"xyz" #endif int main(int argc, const char * argv[]) { @autoreleasepool { NSLog(@"x: %@; y: %@", x, y); NSLog(@"%@", kEnv ? @"abc" : @"cba"); NSLog(@"%@", kVersion ? @"abc" : @"cba"); } return 0; } Before moving forward, you can copy & paste, run, and

Why is this program produces different result with `YES` and `true`?

回眸只為那壹抹淺笑 提交于 2021-02-17 02:06:14
问题 Here is the full program. Can you figure out its console output? #import <Foundation/Foundation.h> #define kEnv YES #if kEnv #define x @"abc" #else #define x @"xyz" #endif #define kVersion true #if kVersion #define y @"abc" #else #define y @"xyz" #endif int main(int argc, const char * argv[]) { @autoreleasepool { NSLog(@"x: %@; y: %@", x, y); NSLog(@"%@", kEnv ? @"abc" : @"cba"); NSLog(@"%@", kVersion ? @"abc" : @"cba"); } return 0; } Before moving forward, you can copy & paste, run, and

Preprocessor : Get Operating System .Net Core

北城以北 提交于 2021-02-09 15:23:22
问题 I'm writing a class that I wish to use on both Windows & Linux. One of the methods that is in this class is accessing the Windows Registry What I'm hoping to achieve is to somehow disable this particular method from being used when using a Linux machine. First of all I did some research to see if there was something for .Net Core that would allow me to check which operating system is in use, I found this and sure enough that works. When I implemented it into my code when accessing a method, I

Preprocessor : Get Operating System .Net Core

谁都会走 提交于 2021-02-09 15:18:51
问题 I'm writing a class that I wish to use on both Windows & Linux. One of the methods that is in this class is accessing the Windows Registry What I'm hoping to achieve is to somehow disable this particular method from being used when using a Linux machine. First of all I did some research to see if there was something for .Net Core that would allow me to check which operating system is in use, I found this and sure enough that works. When I implemented it into my code when accessing a method, I

Preprocessor : Get Operating System .Net Core

旧街凉风 提交于 2021-02-09 15:15:53
问题 I'm writing a class that I wish to use on both Windows & Linux. One of the methods that is in this class is accessing the Windows Registry What I'm hoping to achieve is to somehow disable this particular method from being used when using a Linux machine. First of all I did some research to see if there was something for .Net Core that would allow me to check which operating system is in use, I found this and sure enough that works. When I implemented it into my code when accessing a method, I

Preprocessor : Get Operating System .Net Core

不羁的心 提交于 2021-02-09 15:13:41
问题 I'm writing a class that I wish to use on both Windows & Linux. One of the methods that is in this class is accessing the Windows Registry What I'm hoping to achieve is to somehow disable this particular method from being used when using a Linux machine. First of all I did some research to see if there was something for .Net Core that would allow me to check which operating system is in use, I found this and sure enough that works. When I implemented it into my code when accessing a method, I

How to split this into header and source files?

最后都变了- 提交于 2021-01-28 04:40:16
问题 I have some C code I'd like to split into a header file and a source file: #ifndef BENCHMARK_H #define BENCHMARK_H #ifdef WIN32 #include <windows.h> double get_time() { LARGE_INTEGER t, f; QueryPerformanceCounter(&t); QueryPerformanceFrequency(&f); return (double)t.QuadPart/(double)f.QuadPart; } #else #include <sys/time.h> #include <sys/resource.h> double get_time() { struct timeval t; struct timezone tzp; gettimeofday(&t, &tzp); return t.tv_sec + t.tv_usec*1e-6; } #endif #endif What would be

How to write generic #define macro in C and write less code

烈酒焚心 提交于 2021-01-02 08:20:52
问题 Let's say I have 2 sets of values for P_A, P_B, P_C as below #define X_P_A 2 #define X_P_B 3 #define X_P_C 4 #define Y_P_A 5 #define Y_P_B 6 #define Y_P_C 7 There are 3 types of users:- once that only need X variants, once that only need Y variants and once those may need both. eg #ifdef X #define P_A X_P_A #define P_B X_P_B #define P_C X_P_C #endif #ifdef Y #define P_A Y_P_A #define P_B Y_P_B #define P_C Y_P_C #endif Users that need both will make the decision at run time and call X_P_<> or

How to write generic #define macro in C and write less code

旧时模样 提交于 2021-01-02 08:20:34
问题 Let's say I have 2 sets of values for P_A, P_B, P_C as below #define X_P_A 2 #define X_P_B 3 #define X_P_C 4 #define Y_P_A 5 #define Y_P_B 6 #define Y_P_C 7 There are 3 types of users:- once that only need X variants, once that only need Y variants and once those may need both. eg #ifdef X #define P_A X_P_A #define P_B X_P_B #define P_C X_P_C #endif #ifdef Y #define P_A Y_P_A #define P_B Y_P_B #define P_C Y_P_C #endif Users that need both will make the decision at run time and call X_P_<> or