[cmake-developers] [PATCH] Macro generation for relaxed constexpr
Stephen Kelly
steveire at gmail.com
Wed Jul 8 16:40:03 EDT 2015
Jean-Michaël Celerier wrote:
>> I think there should be a test for the different allowed contexts of the
> ${prefix_arg}_RELAXED_CONSTEXPR and ${prefix_arg}_CONSTEXPR macros. Could
> you extend Tests/Module/WriteCompilerDetectionHeader with a test for that?
>
> For sure, I'll do this asap.
>
>> constexpr foo = ...;
>
> If I read http://en.cppreference.com/w/cpp/language/constexpr
> correctly, there should be no differences in variable handling between
> C++11 and 14 for constexpr, the changes are only for function bodies.
Right.
Qt 5 provides a macro for this context which expands to either 'const' or
'constexpr' depending on whether cxx_constexpr is available, and another
macro which expands to either 'const' or 'constexpr' depending on whether
cxx_relaxed_constexpr is available.
Compare the assembly of the following when compiled with -std=c++98/11/14:
#if __has_feature(cxx_constexpr)
#define DECL_CONSTEXPR constexpr
#else
#define DECL_CONSTEXPR
#endif
#if __has_feature(cxx_relaxed_constexpr)
#define DECL_RELAXED_CONSTEXPR constexpr
#else
#define DECL_RELAXED_CONSTEXPR
#endif
DECL_CONSTEXPR int getNumRegular()
{
return 42;
}
DECL_RELAXED_CONSTEXPR int getNumRelaxed()
{
int result = 0;
for (int i = 0; i < 4; ++i)
result += 10;
return result + 2;
}
DECL_CONSTEXPR int betterIfConstexprParameter(int param)
{
return param / 2;
}
#if __has_feature(cxx_constexpr)
#define CXX11_CONSTEXPR_VARIABLE constexpr
#else
#define CXX11_CONSTEXPR_VARIABLE const
#endif
#if __has_feature(cxx_relaxed_constexpr)
#define CXX14_CONSTEXPR_VARIABLE constexpr
#else
#define CXX14_CONSTEXPR_VARIABLE const
#endif
int main()
{
CXX11_CONSTEXPR_VARIABLE int num1 = getNumRegular();
CXX11_CONSTEXPR_VARIABLE int result1 = betterIfConstexprParameter(num1);
CXX14_CONSTEXPR_VARIABLE int num2 = getNumRelaxed();
CXX14_CONSTEXPR_VARIABLE int result2 = betterIfConstexprParameter(num2);
return result1 - result2;
}
$ wc -l 98.s 11.s 14.s
126 98.s
90 11.s
31 14.s
Of course, such macros don't belong in the same patch as the one you
submitted and could be in a follow-up.
Thanks,
Steve.
More information about the cmake-developers
mailing list