c-preprocessor

How to print C-preprocessor variables like __LINE__ with mexErrMsgTxt() In Matlab MEX

淺唱寂寞╮ 提交于 2020-01-03 00:55:10
问题 For debugging Matlab-MEX, which can be quite a hassle, it would be nice to have better assertion capabilities. Following this question about mex-assertions, it is possible to define a preprocessor makro, that throws an error to Matlab and prints a string (can mostly replace mxAssert , which unfortunately crashes Matlab2011b). #define myassert( isOK,astr ) ( (isOK) ? (void)0 : (void) mexErrMsgTxt(astr) ) It would be much nicer to print the file, line number and caller function, from where

Is it possible to generate a global list of marked strings at compile time/runtime?

非 Y 不嫁゛ 提交于 2020-01-02 18:23:16
问题 So, I'm working on translating my C++ app into multiple languages. What I'm currently using is something like: #define TR(x) (lookupTranslatedString( currentLocale(), x )) wcout << TR(L"This phrase is in English") << endl; The translations are from a CSV file which maps the english string to the translated string. "This phrase is in English","Nasa Tagalog itong pagsabi" This is simplified, but that's the basic idea. My question is about generating the list of English phrases that need to be

Is it possible to generate a global list of marked strings at compile time/runtime?

大兔子大兔子 提交于 2020-01-02 18:23:03
问题 So, I'm working on translating my C++ app into multiple languages. What I'm currently using is something like: #define TR(x) (lookupTranslatedString( currentLocale(), x )) wcout << TR(L"This phrase is in English") << endl; The translations are from a CSV file which maps the english string to the translated string. "This phrase is in English","Nasa Tagalog itong pagsabi" This is simplified, but that's the basic idea. My question is about generating the list of English phrases that need to be

Is the `gnu-zero-variadic-macro-arguments` safe to ignore?

僤鯓⒐⒋嵵緔 提交于 2020-01-02 10:23:22
问题 Consider the following code (live example): #define TEST_VA(mX, ...) TEST #define STRINGIFY_IMPL(mX) #mX #define STRINGIFY(mX) STRINGIFY_IMPL(mX) #include <iostream> int main() { std::cout << STRINGIFY(TEST_VA(1)) << std::endl; std::cout << STRINGIFY(TEST_VA()) << std::endl; return 0; } clang++ 3.4 complains: main.cpp:9:37: warning: must specify at least one argument for '...' parameter of variadic macro [-Wgnu-zero-variadic-macro-arguments] std::cout << STRINGIFY(TEST_VA(1)) << std::endl; ^

Implementing compile-time mechanism checking uniqueness of a string

流过昼夜 提交于 2020-01-02 05:48:07
问题 The simplest way of defining my problem is that I'm trying to implement a mechanism that would check whether the same string had already been used (or a pair (number, string)). I would like this mechanism to be implemented in a smart way using C preprocessor. I would also like that this mechanism gave me compile errors when there is a conflict or run-time errors in Debug mode (by checking assertions). We don't want the developer to make a mistake when adding a message, as every message should

#define causes an “expected primary-expression” error

对着背影说爱祢 提交于 2020-01-02 05:08:07
问题 #define N 10; int main() { int x; for (int i=0; i<N; i++) x = i; return 0; } Result of compiling this in g++: test-define.cpp: In function ‘int main()’: test-define.cpp:7:22: error: expected primary-expression before ‘;’ token test-define.cpp:7:22: error: expected ‘)’ before ‘;’ token test-define.cpp:7:24: error: name lookup of ‘i’ changed for ISO ‘for’ scoping [-fpermissive] test-define.cpp:7:24: note: (if you use ‘-fpermissive’ G++ will accept your code) test-define.cpp:7:27: error:

Embedding JSON as a string in C++ code using preprocessor

人走茶凉 提交于 2020-01-02 04:17:06
问题 I saw a mix of C++ and JSON code in the Chromium project. For example in this file: config/software_rendering_list_json.cc Is the magic with this macro? #define LONG_STRING_CONST(...) #__VA_ARGS__ How does it "stringify" arbitrary JSON content? 回答1: You guessed right! # inside a macro body turns the subsequent token into a C string literal containing that token's text. In this case, the next token is the special __VA_ARGS__ macro keyword that is substituted with all the arguments to the

What is the precedence of operators in C# Preprocessor Directives?

眉间皱痕 提交于 2020-01-02 03:10:36
问题 If I have a piece of code written in C# wrapped in an #if directive, what (if any) precedence is applied to any boolean operators that might be used in that directive? In other words: #if DEBUG || MYTEST && PLATFORM_WINDOWS // ... Some code here #endif Will that be simply evaluated left to right as #if (DEBUG || MYTEST) && PLATFORM_WINDOWS And similarly, would #if PLATFORM_WINDOWS && DEBUG || MYTEST Be evaluated as #if (PLATFORM_WINDOWS && DEBUG) || MYTEST Or is there some precedence order

Checking the sizeof an integer type in the preprocessor

自闭症网瘾萝莉.ら 提交于 2020-01-02 01:01:09
问题 How can I check the size of an unsigned in the preprocessor under g++? sizeof is out of the question since it is not defined when during preprocessing. 回答1: This may not be the most elegant method, but one thing that you may be able to leverage is UINT_MAX defined in "limits.h". That is, ... if UINT_MAX == 65535, then you would know that sizeof (unsigned) = 2 if UINT_MAX == 4294967295, then you would know that sizeof (unsigned) = 4. and so on. As I said, not elegant, but it should provide

How do I use C preprocessor macros with Rust's FFI?

瘦欲@ 提交于 2020-01-02 00:57:35
问题 I'm writing some code that interfaces an existing library written in C. In my Rust code I'd like to be able to use values from CPP macros. If I have a C include.h that looks like this: #define INIT_FLAG 0x00000001 I'd like to be able to use it in Rust like this: #[link(name="mylib")] extern { pub static init_flag: c_int = INIT_FLAG; } I've looked at other FFI code and I see a lot of people duplicating these values in Rust instead of getting them from the FFI. This seems a little brittle, and