5,965 questions
15
votes
3
answers
411
views
Workarounds for using __has_include when it may or may not be defined - Is it valid to pass __has_include as a macro argument?
Often, I'd like to chain preprocessor conditionals involving __has_include (defined in C++17 and C23, and supported in earlier versions as an extension by many compilers (GCC 4.9.2 and up, and Clang ...
0
votes
1
answer
129
views
Removing the commas in a variadic macro
I want to remove the comma characters inside __VA_ARGS__.
I have found a FOR_EACH implementation, which kind of solves it, but gives cluttered error messages when there is a mistake.
My problem has ...
Advice
0
votes
6
replies
232
views
C++ compile type endianness for use in macros
I need to layout a structure in two different ways depending on the endianness of the target platform. Currently I'm using an additional pre-compile phase to run a program to test the endianness, and ...
-4
votes
1
answer
93
views
C++ preprocessor VARARGS - invalid syntax in a NodejS header file
I'm experimenting a bit with trying to build NodeJS 24.11.1 with GCC 13.4.0 on an older OS X system, mostly as a can-it-be-done project.
Regardless of the reasons, among the hurdles that could be ...
5
votes
1
answer
173
views
Why does the same code, one generated by macros and the other handwritten, produces different results in MSVC?
The same code, one generated by macros and the other handwritten, produces different results.I'm lost and don't know how to fix it.
my environment is follow:
vs2019, msvc2019_64, c++14
if using ...
2
votes
1
answer
122
views
C macro compilation fails in Clang with "expected ;" due to phantom comma in clang -E expansion
I wrote my "Result" macro header, using C23 and some of the newest features:
#pragma once
#include <stdio.h>
#include <stdlib.h>
#define __RESULT_EAT_PARENS(...) __VA_ARGS__
...
1
vote
1
answer
64
views
C preprocessor and the DEFER technique
This snippet is mostly from the Cloak wiki, C Preprocessor tricks, tips and idioms, with DEFER2, X1, and X2 added by me for ease of referencing things in this post.
#define EMPTY()
#define DEFER(id) ...
8
votes
6
answers
878
views
Template for function with N inputs
If it's possible, I would like to define a macro that would take number of arguments in function template ARGS_COUNT and make a template with all argument types being enumerated and output being ...
0
votes
2
answers
111
views
variadic macro that references another variadic macro is not expanding the way I want [closed]
I have
#file debug.h
#ifndef PDEBUG
#define PDEBUG(msg ...)\
logger_write(logger_stdout, (struct log) {\
.level_target = LOGLEV_DEBUG,\
.message = msg,\
.properties = strdict_make_from_va(...
3
votes
2
answers
166
views
GCC pragma: error: expected expression before ‘#pragma’
Please consider the following minimal example:
#include <stdio.h>
// Compile with:
// gcc -g -o myprogram main.c
//#define SPECIAL
typedef struct {
int id;
char name[50];
float value;
} ...
-3
votes
1
answer
141
views
Is this a C preprocessor bug? [duplicate]
#include <stdio.h>
#define n 16
#define a -2.0
#define b 2.0
#define c (b-a)/n
int main() {
printf("%lf\n",5.0*c);
}
Outputs 1.250000
But if i change (b-a)/n to b/n-a/n it outputs ...
5
votes
1
answer
139
views
How to create meaningful error messages from _Generic macros?
Lets say I have some manner of type safety macro based on _Generic, which I use to wrap an actual function call to make it type safe:
#include <stdio.h>
void print (int i)
{
printf("%d\n&...
1
vote
1
answer
189
views
#error "Please port gnulib freadahead.c to your platform!" while compiling openwrt
Im trying to build old version of openwrt and Im getting this error #error "Please port gnulib freadahead.c to your platform! Look at the definition of fflush, fread, ungetc on your system, then ...
4
votes
1
answer
184
views
In which situations does the second form of `__has_include` appear?
Problem Description
C++17 introduced the __has_include preprocessor expression, which has the following two forms:
__has_include(header-name)
__has_include(header-name-tokens)
For the first form, ...
0
votes
1
answer
112
views
Conditional compilation depending on the size of data type [duplicate]
This does not answer my need. I do not want to generate an error, I want to adapt the behavior of the software depending on the condition.
Example:
#if ( sizeof(unsigned long) == 4 )
# define a 5
#...