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 -decltype(std::__declval<_Tp>(0)) declval() _NOEXCEPT; +template +_LIBCPP_HIDE_FROM_ABI decltype(std::__declval<_Tp>(0)) declval() _NOEXCEPT { + static_assert(_False, "Calling declval is ill-formed, see [declval]/2."); +} _LIBCPP_END_NAMESPACE_STD Index: libcxx/test/std/utilities/utility/declval/declval.verify.cpp =================================================================== --- /dev/null +++ libcxx/test/std/utilities/utility/declval/declval.verify.cpp @@ -0,0 +1,23 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// + +// template typename add_rvalue_reference::type declval() noexcept; + +#include + +int main() { + if (false) { + int i = std::declval(); // expected-warning@*:* {{code will never be executed}} + (void)i; + } + + return 0; +} +// expected-error-re@*:* {{{{(static_assert|static assertion)}} failed{{.*}}Calling declval is ill-formed, see [declval]/2.}}