[libcxx-commits] [PATCH] D145376: [libc++] add declval failure assertion for instantiation

Igor Zhukov via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Mar 6 06:45:36 PST 2023


fsb4000 updated this revision to Diff 502624.
fsb4000 added a comment.

add a test


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D145376/new/

https://reviews.llvm.org/D145376

Files:
  libcxx/include/__utility/declval.h
  libcxx/test/std/utilities/utility/declval/declval.compile.fail.cpp


Index: libcxx/test/std/utilities/utility/declval/declval.compile.fail.cpp
===================================================================
--- /dev/null
+++ libcxx/test/std/utilities/utility/declval/declval.compile.fail.cpp
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <utility>
+
+// template <class T> typename add_rvalue_reference<T>::type declval() noexcept;
+
+#include <utility>
+
+int main() {
+  if (false) {
+    int i = std::declval<int>();
+  }
+
+  return 0;
+}
Index: libcxx/include/__utility/declval.h
===================================================================
--- libcxx/include/__utility/declval.h
+++ libcxx/include/__utility/declval.h
@@ -26,8 +26,10 @@
 _Tp __declval(long);
 _LIBCPP_SUPPRESS_DEPRECATED_POP
 
-template <class _Tp>
-decltype(std::__declval<_Tp>(0)) declval() _NOEXCEPT;
+template <class _Tp, bool _False = false>
+decltype(std::__declval<_Tp>(0)) declval() _NOEXCEPT {
+    static_assert(_False, "Calling declval is ill-formed, see [declval]/2.");
+}
 
 _LIBCPP_END_NAMESPACE_STD
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145376.502624.patch
Type: text/x-patch
Size: 1384 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20230306/2932ae24/attachment.bin>


More information about the libcxx-commits mailing list