Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add macros for conditional constexpr #152

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Corrade/Utility/Test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,15 @@ if(NOT CMAKE_CXX_FLAGS MATCHES "-std=")
corrade_add_test(UtilityMacrosCpp20Test MacrosCpp20Test.cpp)
set_target_properties(UtilityMacrosCpp20Test PROPERTIES CORRADE_CXX_STANDARD 20)
endif()


if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0") OR
(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0") OR
(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "11.4") OR
(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "19.25"))
corrade_add_test(UtilityTypeTraitsCpp20Test TypeTraitsCpp20Test.cpp)
set_target_properties(UtilityTypeTraitsCpp20Test PROPERTIES CORRADE_CXX_STANDARD 14)
endif()
endif()

# It should return with a non-zero exit code
Expand Down
68 changes: 68 additions & 0 deletions src/Corrade/Utility/Test/TypeTraitsCpp20Test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
This file is part of Corrade.

Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018, 2019, 2020, 2021, 2022
Vladimír Vondruš <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

#include "Corrade/TestSuite/Tester.h"
#include "Corrade/Utility/TypeTraits.h"
#include <cstdio>

namespace Corrade { namespace Utility { namespace Test { namespace {

struct TypeTraitsCpp20Test: TestSuite::Tester {
explicit TypeTraitsCpp20Test();
void isConstantEvaluatedTest();
};

TypeTraitsCpp20Test::TypeTraitsCpp20Test() {
addTests({&TypeTraitsCpp20Test::isConstantEvaluatedTest});
}

#ifdef CORRADE_IS_CONSTANT_EVALUATED
constexpr int constevalHelper(int i) {
if (CORRADE_IS_CONSTANT_EVALUATED)
return i + 1;
else {
std::printf("");
return i + 2;
}
}
#endif

void TypeTraitsCpp20Test::isConstantEvaluatedTest() {
#ifndef CORRADE_IS_CONSTANT_EVALUATED
CORRADE_SKIP("CORRADE_IS_CONSTANT_EVALUATED not supported on this compiler.");
#else
constexpr int retConstant = constevalHelper(0);
static_assert(retConstant == 1, "");
CORRADE_COMPARE(retConstant, 1);
const volatile int arg = 0;
const int retRuntime = constevalHelper(arg);
CORRADE_COMPARE(retRuntime, 2);
#endif
}

}}}}

CORRADE_TEST_MAIN(Corrade::Utility::Test::TypeTraitsCpp20Test)
20 changes: 20 additions & 0 deletions src/Corrade/Utility/TypeTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,26 @@ template<class U> class className { \
enum: bool { value = sizeof(get(std::declval<U>())) == sizeof(char) }; \
}

/** @hideinitializer
@brief C++20 is_constant_evaluated
@m_since{2022,10}

Expands to a predicate determining whether given @cpp constexpr @ce function
is being evaluated at compile-time or not. Under C++14 rules, constexpr
functions may be defined as long as at least some of the code paths are able
to be executed at compile-time. As long as a fallback is present, features
such as SIMD or inline assembly may be used in functions marked constexpr.

This support is available on all C++20 capable compilers, but also certain
ones (Clang 9, GCC 9, MSVC 2022 17.1) that expose the feature as a non-portable
extension. In which case it may be used under C++14 relaxed constexpr rules.
*/
#if (defined(CORRADE_TARGET_CLANG) && !defined(CORRADE_TARGET_APPLE_CLANG) && __clang_major__ >= 9) || (defined(CORRADE_TARGET_APPLE_CLANG) && __clang_major__*100 + __clang_minor__ >= 1104) || (defined(CORRADE_TARGET_GCC) && __GNUC__ >= 9) || (defined(CORRADE_TARGET_MSVC) && _MSC_VER >= 1931)
#define CORRADE_IS_CONSTANT_EVALUATED (__builtin_is_constant_evaluated())
#elif CORRADE_CXX_STANDARD >= 202002
#define CORRADE_IS_CONSTANT_EVALUATED (std::is_constant_evaluated())
#endif

namespace Implementation {
/* As of Eigen 3.4.0, due to these two commits in particular,
https://gitlab.com/libeigen/eigen/-/commit/c0ca8a9fa3e03ad7ecb270adfe760a1bff7c0829
Expand Down