4

I would like to use the generalized lambda capture introduced in C++14 (see Move capture in lambda for an explanation). However, the remainder of my code is C++11-friendly. I would like to do something along the lines of

#ifdef CPP14
// move capture in lambda
#else
// capture by-value
#endif

However, there are no good cross-compiler flags to infer versions. Is there anything anyone can suggest? (other than, of course, defining my own macros)

Community
  • 1
  • 1
  • 3
    Perhaps related: [What changes introduced in C++14 can potentially break a program written in C++11?](http://stackoverflow.com/a/23980931/1708801) and [Can C++ code be valid in both C++03 and C++11 but do different things?](http://stackoverflow.com/questions/23047198/can-c-code-be-valid-in-both-c03-and-c11-but-do-different-things/23063914#23063914) – Shafik Yaghmour Jul 08 '14 at 02:57
  • 1
    GCC apparently has this as a GNU extension. I think Boost.Phoenix has this as well. – chris Jul 08 '14 at 02:59
  • 4
    "there are no good cross-compiler flags to infer versions" - there's always the value of `__cplusplus`. – T.C. Jul 08 '14 at 05:08

1 Answers1

1

Actually T.C. is right, the C++11 FDIS says in "16.8 Predefined macro names [cpp.predefined]" that

The name __cplusplus is defined to the value 201103L when compiling a C++ translation unit.

The footnote states:

It is intended that future versions of this standard will replace the value of this macro with a greater value. Non-conforming com- pilers should use a value with at most five decimal digits.

So going with the following code seems totally legit to me.

#if __cplusplus > 201103L
//c++1y or above
#else
//c++11 or below
#endif

However, some compiler might not follow the standard and you might want to check if the _cplusplus value has been incremented for c++1y.

The GCC, for example, had this flag set to 1 until the version 4.7.0.

If you need more information on the _cplusplus flag, you should take a look at this question

Community
  • 1
  • 1
Theolodis
  • 4,977
  • 3
  • 34
  • 53