mercredi 27 juillet 2016

C++11 constexpr compatibility implementation

In a project, I use the cmake module WriteCompilerDetectionHeader to detect if C++11 features are available or not. The module generate a file with compatibility implementations for some features like static_assert: if the feature is available, the macro declared for the feature expands to that feature; it expands to some fallback otherwise.

The feature constexpr has no compatibility implementation. I tried to provide one and ended up with a double implementation: one for constexpr variables and one for constexpr functions.

In cmake file:

write_compiler_detection_header(
    FILE "${CMAKE_CURRENT_BINARY_DIR}/foo-compilerdetection.h"
    PREFIX Foo
    COMPILERS
        GNU
        MSVC
    FEATURES
        cxx_constexpr
)

In global header:

#include "foo-compilerdetection.h"

#ifdef Foo_COMPILER_CXX_CONSTEXPR
#   define Foo_CONSTEXPR_FUNCTION constexpr
#   define Foo_CONSTEXPR_OBJECT constexpr
#else
#   define Foo_CONSTEXPR_FUNCTION inline
#   define Foo_CONSTEXPR_OBJECT const
#endif

So for variables constexpr will be replaced with the qualifier const and for functions it will be replaced with the qualifier inline.

Are these substitutions adequat? Would an other qualifier or combination or qualifiers suit better and possibly work for both variables and functions?

Aucun commentaire:

Enregistrer un commentaire