All Questions

Tagged with
Filter by
Sorted by
Tagged with
161 votes
2 answers
54k views

Is #pragma once part of the C++11 standard?

Traditionally, the standard and portable way to avoid multiple header inclusions in C++ was/is to use the #ifndef - #define - #endifpre-compiler directives scheme also called macro-guard scheme (see ...
101010's user avatar
  • 42.3k
141 votes
3 answers
74k views

When should I prefer constexpr variables over macros?

Where should I prefer using macros and where should I prefer constexpr? Aren't they basically the same? #define MAX_HEIGHT 720 vs constexpr unsigned int max_height = 720;
Tom Dorone's user avatar
  • 1,605
32 votes
2 answers
20k views

Expand macro inside string literal

What I'm trying to do is to #define a macro: #define a(2) and later use it inside a string literal: string = "a";. I want that string to be interpreted not as string but to get the value of a, i.e. ...
Amine's user avatar
  • 347
31 votes
3 answers
14k views

Are empty macro arguments legal in C++11?

I sometimes deliberately omit macro arguments. For example, for a function-like macro like #define MY_MACRO(A, B, C) ... I might call it as: MY_MACRO(, bar, baz) There are still technically 3 ...
SuperElectric's user avatar
28 votes
4 answers
2k views

How to allow copy elision construction for C++ classes (not just POD C structs)

Consider the following code: #include <iostream> #include <type_traits> struct A { A() {} A(const A&) { std::cout << "Copy" << std::endl; } A(A&&) { std::...
Clinton's user avatar
  • 22.8k
24 votes
3 answers
4k views

C++ macro '##' doesn't work after '->' operator

I have a shared_ptr object x, which has get and set methods as follows: x->a_value(); x->set_a_value(); x->b_value(); x->set_b_value(); When i try to define a macro: #define MAC(...
250's user avatar
  • 659
23 votes
1 answer
5k views

Understanding DEFER and OBSTRUCT macros

I created a small macro metaprogramming library that implements basic useful constructs such as REPEAT(times, x), IF(value, true, false), tuples, and more. Most of my implementations work by ...
Vittorio Romeo's user avatar
21 votes
1 answer
10k views

PyBind11 Template Class of Many Types

I'd like to use PyBind11 to wrap a specialized array class. However, the array is available in many flavours (one per each plain-old-datatype). The code looks like this: py::class_<Array2D<...
Richard's user avatar
  • 58.6k
21 votes
6 answers
18k views

Display integer at compile time in static_assert()

Here is a simplified version of what I'm trying to do enum First { a, b, c, nbElementFirstEnum, }; enum Second { a, b, c, nbElementSecondEnum, }; static_assert( ...
b3nj1's user avatar
  • 677
17 votes
3 answers
5k views

C++11 nested macro invocation?

It says in C++ std 16.3.4: The resulting preprocessing token sequence [from a macro invocation replacement] is rescanned, along with all subsequent preprocessing tokens of the source file, for ...
Andrew Tomazos's user avatar
10 votes
3 answers
7k views

What does the "__cplusplus" macro expand to?

What does the C++ macro __cplusplus contain and expand to? Does the macro __cplusplus always, even in oldest C++ implementation, contain and expand to a numeric value? Is it safe to use #if ...
Top-Master's user avatar
  • 8,208
10 votes
3 answers
4k views

C++ or macro magic to generate method and forward arguments

I would like to create a magical macro, or anything, that would generate a something like this: MAGICAL_MACRO(return_type, method_name, ...) should work like this: MAGICAL_MACRO(void, Foo, int a, ...
István Csanády's user avatar
10 votes
1 answer
9k views

How to single out the first parameter sent to a macro taking only a variadic parameter

I try to get at the first actual parameter sent to a variadic macro. This is what I tried, and which does not work in VS2010: #define FIRST_ARG(N, ...) N #define MY_MACRO(...) decltype(FIRST_ARG(...
Bengt Gustafsson's user avatar
7 votes
2 answers
10k views

Getting too many arguments provided to function-like macro invocation compile error while defining lambda inside assert (assert.h) in Xcode [c++]

I am using assertion macro from assert.h I have defined lambda to perform assertion checking. int val1 = 0; int val2 = 1; const auto check = [val1,val2]()-> bool { return val1 < val2; }; //...
T M's user avatar
  • 3,255
7 votes
5 answers
3k views

how to check if function exists

I want to implement my own std::make_unique function with the function being part of std namespace. I know this helper function is added to C++14 but I do not have it in C++11. So, I want to check it ...
Gasim's user avatar
  • 7,775
7 votes
3 answers
247 views

Can Template / Preprocessor hackery be used to support variable arguments in the middle of an argument list?

I have stumbled upon old code that looks like this: void dothing(bool testBool, const std::string& testString1, const std::string& file, int line, ...
rationalcoder's user avatar
7 votes
2 answers
361 views

Is it possible to write a macro existenceof(S,f) or hasfield(S,f) in C++11?

I'm reasonably familiar with the standard metaprogramming solutions (e.g. C++11 ways of finding if a type has member function or supports operator?) which don't involve macro magic. However, I have a ...
Quuxplusone's user avatar
  • 24.8k
6 votes
3 answers
432 views

How can I create a macro which uses a value multiple times, without copying it?

I'd like to create a macro which unpacks a pair into two local variables. I'd like to not create a copy of the pair if it's just a variable, which this would accomplish: #define UNPACK_PAIR(V1, V2, ...
Claudiu's user avatar
  • 227k
6 votes
3 answers
666 views

Find unexecuted lines of c++ code

As part of my unit testing I want to ensure code coverage of the tests. The aim is to place something like REQUIRE_TEST macros somewhere in the code and check whether all of these were called. void ...
example's user avatar
  • 3,379
5 votes
4 answers
3k views

Return from calling function inside lambda

Lambdas are an awesome way to create reusable code inside a function/method without polluting the parent class. They're a very functional replacement for C-style macros most of the time. However, ...
Phlucious's user avatar
  • 3,791
5 votes
4 answers
865 views

macro for switching between auto_ptr and unique_ptr

In a project that still uses pre-C++11 I wanted to prepare the source for the switch by compiling with a C++11 compiler and fixing the errors. They consisted of instances of std::auto_ptr<T> ...
rwst's user avatar
  • 2,585
5 votes
4 answers
3k views

single expression helper for compile-time enforced constexpr function evaluation possible?

@cyberpunk_ is trying to achieve something and made some questions about it but all the chase boils down to this: Is it possible to build a tool to enforce compile-time evaluation of a constexpr ...
oblitum's user avatar
  • 11.7k
5 votes
4 answers
3k views

Custom `assert` macro that supports commas and error message

I would like to create a custom version of the assert macro defined in <cassert>, that displays an error message when the assertion fails. Desired usage: custom_assert(AClass<T1, T2>::...
Vittorio Romeo's user avatar
4 votes
6 answers
7k views

How to use #ifndef with macro argument?

I have a simple code in header.h- #define SWAP(a, b) {a ^= b; b ^= a; a ^= b;} This header.h is included in a code.c file, but my requirement is- I want SWAP to be checked first like- #ifndef ...
A. Gupta's user avatar
4 votes
3 answers
428 views

Getting rid of an ugly C construct

I have inherited a (large) piece of code which has an error tracking mechanism where they pass in a boolean variable to all the methods they call and on errors at various stages of execution the ...
Ferenc Deak's user avatar
  • 34.8k
4 votes
2 answers
4k views

Change a string macro at compilation time

I'm developing an unique client that has to work on different machines. In every machine the server is running in a different IP address, but this address is known. I don't want to tell the client ...
ShakMR's user avatar
  • 115
4 votes
2 answers
300 views

Macro compiles with GCC but not with VS11

I have written the following code to assist restricting my template functions to certain types, with meaningful error message when other types were used. I had the idea from a different question in ...
SalemD's user avatar
  • 118
4 votes
3 answers
5k views

How to extract __VA_ARGS__?

I hava a macro to call static function for each args. For example: #define FOO(X) X::do(); #define FOO_1(X,Y) X::do(); Y::do(); My question is that I need to use foo with variable number of ...
Kennir's user avatar
  • 541
4 votes
1 answer
235 views

C+11-Like Type Decay in Rust

In C++11, you can decay a generic type to a value type, removing reference/rvalue semantics and cv-qualifiers, e.g. decay<int>::type // type is `int` decay<const int&>::type // type ...
Donald Whyte's user avatar
4 votes
2 answers
2k views

Is there a cross platform way to detect debug mode compilation?

Is there a cross platform way to detect debug mode compilation? If not, then how to do it for the top compilers; MSVC, GNU & MINGW, mac, clang, intel. For example MSVC you can detect debug mode ...
EddieV223's user avatar
  • 5,163
4 votes
2 answers
1k views

Is it possible to detect namespace membership in C++?

For C++ types, the <type_traits> header gives us many useful compile-time reflection capabilities. E.g. std::is_base_of<B, D>::value determines at compile-time whether B is a base class of ...
TemplateRex's user avatar
  • 69.9k
4 votes
2 answers
1k views

C++ macro to test availability of __func__, __FUNCTION__, __PRETTY_FUNCTION__, etc

Various modern C/C++ compilers include one or both of __func__ / __FUNCTION__ for purposes of logging the currently executing function. MSVC++ also includes __FUNCSIG__ and GCC __PRETTY_FUNCTION__ as ...
HunterZ's user avatar
  • 191
4 votes
1 answer
331 views

instantiating template functions and classes with boost preprocess

Working with the seemingly standard w, x, y, z demos, suppose I have the following macro trying to be converted to an "iterable" preprocessor macro #define INSTANTIATE_FUNC(rtype, func_name, ...) \ ...
svenevs's user avatar
  • 843
3 votes
3 answers
645 views

"Beautifying" macro

Watching Herb Sutter's talk about atomics at C++ and beyond I glimpsed at his idea of an easy to use lock/unlock mechanism that may or may not come in a future standard of the language. The mechanism ...
user1233963's user avatar
  • 1,480
3 votes
2 answers
165 views

Need explanation over the behavior of C++ code

I need a little explanation over how this C++ code is behaving and returning value #include<iostream> using namespace std; #define MY_MACRO(n) #n #define SQR(x) x * x int main() { //cout<...
Saurabh Sharma's user avatar
3 votes
3 answers
2k views

Variadic macros

Is there any way to write a macro like this: #define G(x1, x2, ... , xn) f(x1), f(x2), ... , f(xn) Or do I need to define this for each individual n? C++0x answers are ok. Edit: I'm asking how to ...
Clinton's user avatar
  • 22.8k
3 votes
2 answers
1k views

What's the difference between the noexcept keyword and _NOEXCEPT macro?

I'm a beginner to C++ and I'm learning about some of the C++11 features. One thing I noticed was that in some parts of the Visual C++ stdlib, the authors used the _NOEXCEPT macro instead of the ...
James Ko's user avatar
  • 33.4k
3 votes
2 answers
494 views

Any clean way to get OpenMP pragmas to work with macros?

I have to add OpenMP to a customer's code. It has a couple of macros that work sort of like so: int i, imax; #ifdef MAC1 double x1, y1 #endif #ifdef MAC2 double x2, y2 #endif //first loop: for (i=0;...
bob.sacamento's user avatar
3 votes
3 answers
267 views

Is there a macro or provision to check is the line of code is inside a class?

I have a c style macro in my code, which prints the logs. I want to alter the macro to print the this pointer. But, there are some portion of a code, which is not a member function of a class or some ...
Balamurugan A's user avatar
3 votes
1 answer
221 views

Throwing function-like variadic macro wrapping, replacing thrown exception

Suppose I have a third party library that provides a function-like ThirdPartyMacro macro that is: Variadic and accepts arbitrary tokens, not just well formed c++ expressions. After parsing the ...
yuri kilochek's user avatar
3 votes
2 answers
5k views

Replace characters in preprocessor definition

I have a definition which includes a path (with no escape sequence) like this one: // Incorrect #define PATH "c:\blah\blah\file.cfg" I would rather like it as this: // Corrected #define PATH "c:\\...
sorush-r's user avatar
  • 10.7k
3 votes
2 answers
630 views

In C++, static assert that a declaration at current position would not be enclosed in any namespace?

In C++ one sometimes has annoying bugs where someone forgot to close out a namespace which was opened in a header file, and it can sometimes be difficult to track down exactly which one. Somewhat ...
Chris Beck's user avatar
  • 15.9k
3 votes
4 answers
160 views

Defining macro improving syntax of specific function

I've created a function declared as: template <typename Container, typename Task> void parallel_for_each(Container &container, Task task, unsigned number_of_threads = ...
Rames's user avatar
  • 918
3 votes
2 answers
2k views

Expanding macro inside raw string

I would like to do some debugging of my crazy macros, but there's no way to do it because macros generate code, not strings. I'd have to change the macros to emit strings in order for my program to ...
Steven Lu's user avatar
  • 42.3k
3 votes
2 answers
145 views

Is there some way to determine whether the context allows the use of "this"?

Is there some way to determine whether the context allows the use of "this"? My goal is write a generic macro, for logging, which depending on the context use "this" (for instance, for print the ...
user1476999's user avatar
3 votes
2 answers
402 views

How to detect if VS C++ compiler supports C++11?

How to detect if Visual Studio (VS) C++ compiler supports C++11 through preprocessor macros? I tried with __cplusplus (the preprocessor macro that many people advise to use for this kind of checks) ...
user avatar
3 votes
1 answer
154 views

Get subsets of macro values using offset

I have a list defined as a preprocessor value #define LIST 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. I want to write a macro that gets an index 0 or 1 and evaluates to a subset of the LIST such that for index 0 ...
e271p314's user avatar
  • 3,889
3 votes
1 answer
977 views

Compiler macro to test difference between uint64_t and unsigned long long int

I have C++11 code that is failing to compile because an overloaded function is redefined with the same types as arguments: char const* foo(uint64_t) { return "%" PRIu64; } char const* foo(unsigned ...
Alex Reynolds's user avatar
3 votes
2 answers
595 views

Checking for availability of C++0x algorithm additions

I'm trying to figure out which of the additions to the algorithm headers are supported by a given implementation (gcc and MSVC would be enough). The simple way would be to do it the same way as one ...
pmr's user avatar
  • 59.3k
3 votes
0 answers
521 views

Function argument as __builtin_constant_p?

Is it possible to have a function argument which is __builtin_constant_p? This macro works fine: #define FOO(a) {\ static_assert(__builtin_constant_p(a));\ } void bar() { FOO("abc"); } I ...
Jan Rüegg's user avatar
  • 9,787