Is it [binary] safe do to this ?
struct Foo { #if __cplusplus > 199711L Foo( std::initializer_list<int> & list ) { /* ... */ } #endif };
I have seen this topic, however the OP original question isn't answered.
Is there a better way to achieve this behavior ?

- 1
- 1

- 7,055
- 7
- 46
- 77
-
What do you mean by "binary safe"? You must of course compile *all* your code with the exact same macro definitions. – Kerrek SB Jul 27 '12 at 15:26
-
@KerrekSB A library compiled with -std=c++11, using c++11 features only in conditional inline member functions, won't work if the user isn't compiling its application with -std=c++11 ? – Maël Nison Jul 27 '12 at 15:29
-
1It's definitely ill-formed as far as the language standard is concerned. Here's a [related question of mine](http://stackoverflow.com/questions/10717106/can-different-gcc-dialects-be-linked-together) on the subject. – Kerrek SB Jul 27 '12 at 15:34
-
2I don't think it's ill-formed as far as the language standard is concerned, because the language standard simply doesn't address it. If a header's contents only change depending on `__cplusplus`, then as far as the standard is concerned, the header's contents don't change at all, because the value of `__cplusplus` doesn't change. Interoperability with other languages, including earlier versions of C++, is beyond the scope of the standard. – Jul 28 '12 at 20:57
2 Answers
Its probably fine, DirectX structs are implemented this way, but there its done to support both C and C++.

- 12,709
- 2
- 32
- 59
I assume that you:
have such code being compiled into a library, have been upgrading your compiler(s) and now want to compile your code with C++11 (or cannot because you don't have the code), or,
have code already in a library from before C++11 and now that you have upgraded your compiler(s) you want to use the above code and be "compatible" with the old library, or,
want to compile code without C++11 and link it with code compiled with C++11 using the same compiler.
In each case, the answer is not a C++ question, rather, it is a compiler ABI question since it concerns whether or not (i) the link phase will work and (ii) the run-time forms of classes, etc. are still valid between the compilers/versions/compiler settings.
So you will need to examine the documentation for the compiler(s) you are using to find out if it is "binary safe".
NOTE #1: If your compiler(s) has(/have) changed the exception handling emitted code design, RTTI layout of classes, and/or the name mangling scheme in any way, then your answer is, "No it is not safe." But these are likely not the only cases.
NOTE #2: If it is with the same compiler and different settings (e.g., with C++11 and without) then you technically are violating ODR (one definition rule) assumptions when you exclude code for some modules and have it for others within the same program. In this case, the results are technically implementation defined, but, since it is not virtual, it is very, very, very likely to work with most compilers provided only one version of one compiler is being used.

- 1,283
- 11
- 10