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

C++20 is_constant_evaluated #17

Open
kunaltyagi opened this issue Sep 29, 2019 · 0 comments
Open

C++20 is_constant_evaluated #17

kunaltyagi opened this issue Sep 29, 2019 · 0 comments

Comments

@kunaltyagi
Copy link

C++20 comes with a handy is_constant_evaluated which can be used to detect evaluation context.

Essentially, it can allow gcem to be a complete wrapper around stdlib operations (where stdlib is being replaced for compile-time context) and better runtime performance.

Moreover, without depending on C++ version, __cpp_lib_is_constant_evaluated can be used to check if the compiler provides the function (from header <type_traits>)

Before:

template<typename T>
constexpr
return_t<T>
ceil(const T x)
noexcept
{
    return internal::ceil_check( static_cast<return_t<T>>(x) );
}

After:

template<typename T>
constexpr
return_t<T>
ceil(const T x)
noexcept
{
#ifdef __cpp_lib_is_constant_evaluated
    if (!std::is_constant_evaluated()) return std::ceil(static_cast<return_t<T>>(x));
#endif
    return internal::ceil_check( static_cast<return_t<T>>(x) );
}

Benefits:

  • GCEM still remains C++11 compliant, but provides functionality if the compiler can via extensions
  • We can use GCEM without incurring a penalty if a run-time context is used where compile-time was expected

Cons: Slight more code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant