[libcxx-commits] [libcxx] r364873 - Add a private call '__libcpp_is_constant_evaluated' which 'works' for old language versions and w/o any compiler support. 'Working', in this case, means that it returns false in those cases.

Marshall Clow via libcxx-commits libcxx-commits at lists.llvm.org
Mon Jul 1 16:16:47 PDT 2019


Author: marshall
Date: Mon Jul  1 16:16:46 2019
New Revision: 364873

URL: http://llvm.org/viewvc/llvm-project?rev=364873&view=rev
Log:
Add a private call '__libcpp_is_constant_evaluated' which 'works' for old language versions and w/o any compiler support. 'Working', in this case, means that it returns false in those cases.

Added:
    libcxx/trunk/test/libcxx/type_traits/is_constant_evaluated.pass.cpp
Modified:
    libcxx/trunk/include/type_traits

Modified: libcxx/trunk/include/type_traits
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/type_traits?rev=364873&r1=364872&r2=364873&view=diff
==============================================================================
--- libcxx/trunk/include/type_traits (original)
+++ libcxx/trunk/include/type_traits Mon Jul  1 16:16:46 2019
@@ -3999,13 +3999,18 @@ enum class endian
 };
 #endif
 
-#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_BUILTIN_IS_CONSTANT_EVALUATED)
+#ifndef _LIBCPP_HAS_NO_BUILTIN_IS_CONSTANT_EVALUATED
+#if _LIBCPP_STD_VER > 17
 _LIBCPP_INLINE_VISIBILITY
 inline constexpr bool is_constant_evaluated() noexcept {
   return __builtin_is_constant_evaluated();
 }
 #endif
 
+_LIBCPP_CONSTEXPR bool __libcpp_is_constant_evaluated() _NOEXCEPT { return __builtin_is_constant_evaluated(); }
+#else
+_LIBCPP_CONSTEXPR bool __libcpp_is_constant_evaluated() _NOEXCEPT { return false; }
+#endif
 
 template <class _CharT>
 using _IsCharLikeType = _And<is_standard_layout<_CharT>, is_trivial<_CharT> >;

Added: libcxx/trunk/test/libcxx/type_traits/is_constant_evaluated.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/type_traits/is_constant_evaluated.pass.cpp?rev=364873&view=auto
==============================================================================
--- libcxx/trunk/test/libcxx/type_traits/is_constant_evaluated.pass.cpp (added)
+++ libcxx/trunk/test/libcxx/type_traits/is_constant_evaluated.pass.cpp Mon Jul  1 16:16:46 2019
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+
+// <type_traits>
+
+// __libcpp_is_constant_evaluated()
+
+// returns false when there's no constant evaluation support from the compiler.
+//  as well as when called not in a constexpr context
+
+#include <type_traits>
+#include <cassert>
+
+#include "test_macros.h"
+
+int main (int, char**) {
+    ASSERT_SAME_TYPE(decltype(std::__libcpp_is_constant_evaluated()), bool);
+    ASSERT_NOEXCEPT(std::__libcpp_is_constant_evaluated());
+
+#if !defined(_LIBCPP_HAS_NO_BUILTIN_IS_CONSTANT_EVALUATED) && !defined(_LIBCPP_CXX03_LANG)
+    static_assert(std::__libcpp_is_constant_evaluated(), "");
+#endif
+
+    bool p = std::__libcpp_is_constant_evaluated();
+    assert(!p);
+
+    return 0;
+    }




More information about the libcxx-commits mailing list