2020-12-10 18:28:13 -05:00
|
|
|
// -*- C++ -*-
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef _LIBCPP___MEMORY_POINTER_TRAITS_H
|
|
|
|
#define _LIBCPP___MEMORY_POINTER_TRAITS_H
|
|
|
|
|
|
|
|
#include <__config>
|
2021-06-12 06:13:44 +00:00
|
|
|
#include <__memory/addressof.h>
|
2022-12-21 00:07:17 +01:00
|
|
|
#include <__type_traits/conditional.h>
|
|
|
|
#include <__type_traits/conjunction.h>
|
|
|
|
#include <__type_traits/decay.h>
|
|
|
|
#include <__type_traits/is_class.h>
|
|
|
|
#include <__type_traits/is_function.h>
|
|
|
|
#include <__type_traits/is_void.h>
|
|
|
|
#include <__type_traits/void_t.h>
|
|
|
|
#include <__utility/declval.h>
|
2022-08-10 11:34:31 -04:00
|
|
|
#include <cstddef>
|
2020-12-10 18:28:13 -05:00
|
|
|
|
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
2022-02-01 20:16:40 -05:00
|
|
|
# pragma GCC system_header
|
2020-12-10 18:28:13 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
|
|
|
template <class _Tp, class = void>
|
|
|
|
struct __has_element_type : false_type {};
|
|
|
|
|
|
|
|
template <class _Tp>
|
2022-10-01 15:42:00 +02:00
|
|
|
struct __has_element_type<_Tp, __void_t<typename _Tp::element_type> > : true_type {};
|
2020-12-10 18:28:13 -05:00
|
|
|
|
|
|
|
template <class _Ptr, bool = __has_element_type<_Ptr>::value>
|
2023-09-18 05:46:59 -07:00
|
|
|
struct __pointer_traits_element_type {};
|
2020-12-10 18:28:13 -05:00
|
|
|
|
|
|
|
template <class _Ptr>
|
|
|
|
struct __pointer_traits_element_type<_Ptr, true> {
|
2021-08-31 10:32:11 -04:00
|
|
|
typedef _LIBCPP_NODEBUG typename _Ptr::element_type type;
|
2020-12-10 18:28:13 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
template <template <class, class...> class _Sp, class _Tp, class... _Args>
|
|
|
|
struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true> {
|
2021-08-31 10:32:11 -04:00
|
|
|
typedef _LIBCPP_NODEBUG typename _Sp<_Tp, _Args...>::element_type type;
|
2020-12-10 18:28:13 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
template <template <class, class...> class _Sp, class _Tp, class... _Args>
|
|
|
|
struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false> {
|
2021-08-31 10:32:11 -04:00
|
|
|
typedef _LIBCPP_NODEBUG _Tp type;
|
2020-12-10 18:28:13 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class _Tp, class = void>
|
|
|
|
struct __has_difference_type : false_type {};
|
|
|
|
|
|
|
|
template <class _Tp>
|
2022-10-01 15:42:00 +02:00
|
|
|
struct __has_difference_type<_Tp, __void_t<typename _Tp::difference_type> > : true_type {};
|
2020-12-10 18:28:13 -05:00
|
|
|
|
|
|
|
template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
|
|
|
|
struct __pointer_traits_difference_type {
|
2021-08-31 10:32:11 -04:00
|
|
|
typedef _LIBCPP_NODEBUG ptrdiff_t type;
|
2020-12-10 18:28:13 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class _Ptr>
|
|
|
|
struct __pointer_traits_difference_type<_Ptr, true> {
|
2021-08-31 10:32:11 -04:00
|
|
|
typedef _LIBCPP_NODEBUG typename _Ptr::difference_type type;
|
2020-12-10 18:28:13 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class _Tp, class _Up>
|
|
|
|
struct __has_rebind {
|
|
|
|
private:
|
2022-06-10 12:42:46 +02:00
|
|
|
template <class _Xp>
|
|
|
|
static false_type __test(...);
|
2020-12-10 18:28:13 -05:00
|
|
|
_LIBCPP_SUPPRESS_DEPRECATED_PUSH
|
2022-06-10 12:42:46 +02:00
|
|
|
template <class _Xp>
|
|
|
|
static true_type __test(typename _Xp::template rebind<_Up>* = 0);
|
2020-12-10 18:28:13 -05:00
|
|
|
_LIBCPP_SUPPRESS_DEPRECATED_POP
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2020-12-10 18:28:13 -05:00
|
|
|
public:
|
2022-06-10 12:42:46 +02:00
|
|
|
static const bool value = decltype(__test<_Tp>(0))::value;
|
2020-12-10 18:28:13 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
|
|
|
|
struct __pointer_traits_rebind {
|
|
|
|
#ifndef _LIBCPP_CXX03_LANG
|
2021-08-31 10:32:11 -04:00
|
|
|
typedef _LIBCPP_NODEBUG typename _Tp::template rebind<_Up> type;
|
2020-12-10 18:28:13 -05:00
|
|
|
#else
|
2021-08-31 10:32:11 -04:00
|
|
|
typedef _LIBCPP_NODEBUG typename _Tp::template rebind<_Up>::other type;
|
2020-12-10 18:28:13 -05:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
template <template <class, class...> class _Sp, class _Tp, class... _Args, class _Up>
|
|
|
|
struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true> {
|
|
|
|
#ifndef _LIBCPP_CXX03_LANG
|
2021-08-31 10:32:11 -04:00
|
|
|
typedef _LIBCPP_NODEBUG typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
|
2020-12-10 18:28:13 -05:00
|
|
|
#else
|
2021-08-31 10:32:11 -04:00
|
|
|
typedef _LIBCPP_NODEBUG typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
|
2020-12-10 18:28:13 -05:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
template <template <class, class...> class _Sp, class _Tp, class... _Args, class _Up>
|
|
|
|
struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false> {
|
|
|
|
typedef _Sp<_Up, _Args...> type;
|
|
|
|
};
|
|
|
|
|
2023-09-18 05:46:59 -07:00
|
|
|
template <class _Ptr, class = void>
|
|
|
|
struct __pointer_traits_impl {};
|
|
|
|
|
2020-12-10 18:28:13 -05:00
|
|
|
template <class _Ptr>
|
2023-09-18 05:46:59 -07:00
|
|
|
struct __pointer_traits_impl<_Ptr, __void_t<typename __pointer_traits_element_type<_Ptr>::type> > {
|
|
|
|
typedef _Ptr pointer;
|
|
|
|
typedef typename __pointer_traits_element_type<pointer>::type element_type;
|
|
|
|
typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
|
2020-12-10 18:28:13 -05:00
|
|
|
|
|
|
|
#ifndef _LIBCPP_CXX03_LANG
|
|
|
|
template <class _Up>
|
|
|
|
using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
|
|
|
|
#else
|
|
|
|
template <class _Up>
|
|
|
|
struct rebind {
|
|
|
|
typedef typename __pointer_traits_rebind<pointer, _Up>::type other;
|
|
|
|
};
|
2021-04-20 12:03:32 -04:00
|
|
|
#endif // _LIBCPP_CXX03_LANG
|
2020-12-10 18:28:13 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
struct __nat {};
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2020-12-10 18:28:13 -05:00
|
|
|
public:
|
2022-08-19 13:08:01 +02:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static pointer
|
2022-10-01 15:42:00 +02:00
|
|
|
pointer_to(__conditional_t<is_void<element_type>::value, __nat, element_type>& __r) {
|
2020-12-10 18:28:13 -05:00
|
|
|
return pointer::pointer_to(__r);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-09-18 05:46:59 -07:00
|
|
|
template <class _Ptr>
|
|
|
|
struct _LIBCPP_TEMPLATE_VIS pointer_traits : __pointer_traits_impl<_Ptr> {};
|
|
|
|
|
2020-12-10 18:28:13 -05:00
|
|
|
template <class _Tp>
|
|
|
|
struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*> {
|
|
|
|
typedef _Tp* pointer;
|
|
|
|
typedef _Tp element_type;
|
|
|
|
typedef ptrdiff_t difference_type;
|
|
|
|
|
|
|
|
#ifndef _LIBCPP_CXX03_LANG
|
|
|
|
template <class _Up>
|
|
|
|
using rebind = _Up*;
|
|
|
|
#else
|
|
|
|
template <class _Up>
|
|
|
|
struct rebind {
|
|
|
|
typedef _Up* other;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct __nat {};
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2020-12-10 18:28:13 -05:00
|
|
|
public:
|
2022-08-19 13:08:01 +02:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static pointer
|
2022-10-01 15:42:00 +02:00
|
|
|
pointer_to(__conditional_t<is_void<element_type>::value, __nat, element_type>& __r) _NOEXCEPT {
|
2020-12-10 18:28:13 -05:00
|
|
|
return std::addressof(__r);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifndef _LIBCPP_CXX03_LANG
|
2022-09-06 00:33:34 +02:00
|
|
|
template <class _From, class _To>
|
|
|
|
using __rebind_pointer_t = typename pointer_traits<_From>::template rebind<_To>;
|
2020-12-10 18:28:13 -05:00
|
|
|
#else
|
2022-09-06 00:33:34 +02:00
|
|
|
template <class _From, class _To>
|
|
|
|
using __rebind_pointer_t = typename pointer_traits<_From>::template rebind<_To>::other;
|
2020-12-10 18:28:13 -05:00
|
|
|
#endif
|
|
|
|
|
2021-01-15 12:59:56 -05:00
|
|
|
// to_address
|
|
|
|
|
2021-05-04 18:51:58 -04:00
|
|
|
template <class _Pointer, class = void>
|
|
|
|
struct __to_address_helper;
|
2021-01-15 12:59:56 -05:00
|
|
|
|
|
|
|
template <class _Tp>
|
2021-05-04 18:51:58 -04:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp* __to_address(_Tp* __p) _NOEXCEPT {
|
2021-01-15 12:59:56 -05:00
|
|
|
static_assert(!is_function<_Tp>::value, "_Tp is a function type");
|
|
|
|
return __p;
|
|
|
|
}
|
|
|
|
|
2022-08-04 10:57:58 -07:00
|
|
|
template <class _Pointer, class = void>
|
|
|
|
struct _HasToAddress : false_type {};
|
|
|
|
|
|
|
|
template <class _Pointer>
|
[libc++][NFC] Qualify declval
While it's not necessary to qualify calls to `declval` it makes error messages very crypric if the declaration isn't reachable anymore
For example:
```
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:53:66: error: no type named 'type' in 'std::common_type<long, long>'
typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__type_traits/common_type.h:107:14: note: in instantiation of template class 'std::common_type<std::chrono::duration<long, std::ratio<3600, 1>>, std::chrono::duration<long, std::ratio<3600, 1>>>' requested here
: public common_type<_Tp, _Tp> {};
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:279:58: note: in instantiation of template class 'std::common_type<std::chrono::duration<long, std::ratio<3600, 1>>>' requested here
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator+() const {return typename common_type<duration>::type(*this);}
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:308:54: note: in instantiation of template class 'std::chrono::duration<long, std::ratio<3600, 1>>' requested here
typedef duration< int, ratio_multiply<ratio<24>, hours::period>> days;
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:280:81: error: no type named 'type' in 'std::common_type<std::chrono::duration<long, std::ratio<3600, 1>>>'
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator-() const {return typename common_type<duration>::type(-__rep_);}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:308:54: note: in instantiation of template class 'std::chrono::duration<long, std::ratio<3600, 1>>' requested here
typedef duration< int, ratio_multiply<ratio<24>, hours::period>> days;
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:53:66: error: no type named 'type' in 'std::common_type<int, int>'
typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__type_traits/common_type.h:107:14: note: in instantiation of template class 'std::common_type<std::chrono::duration<int, std::ratio<86400, 1>>, std::chrono::duration<int, std::ratio<86400, 1>>>' requested here
: public common_type<_Tp, _Tp> {};
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:279:58: note: in instantiation of template class 'std::common_type<std::chrono::duration<int, std::ratio<86400, 1>>>' requested here
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator+() const {return typename common_type<duration>::type(*this);}
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:309:55: note: in instantiation of template class 'std::chrono::duration<int, std::ratio<86400, 1>>' requested here
typedef duration< int, ratio_multiply<ratio<7>, days::period>> weeks;
^
19 similar errors omitted
```
changes with qualification added to:
```
While building module 'std' imported from /home/nikolask/llvm-projects/libcxx/libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/common_type.pass.cpp:13:
In file included from <module-includes>:17:
In file included from /home/nikolask/llvm-projects/libcxx/build/include/c++/v1/math.h:309:
In file included from /home/nikolask/llvm-projects/libcxx/build/include/c++/v1/limits:107:
In file included from /home/nikolask/llvm-projects/libcxx/build/include/c++/v1/type_traits:432:
In file included from /home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__type_traits/common_reference.h:13:
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__type_traits/common_type.h:28:43: error: declaration of 'declval' must be imported from module 'std.utility.__utility.declval' before it is required
using __cond_type = decltype(false ? std::declval<_Tp>() : std::declval<_Up>());
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__utility/declval.h:30:34: note: declaration here is not visible
decltype(std::__declval<_Tp>(0)) declval() _NOEXCEPT;
^
/home/nikolask/llvm-projects/libcxx/libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/common_type.pass.cpp:13:10: fatal error: could not build module 'std'
#include <functional>
~~~~~~~~^
2 errors generated.
```
Reviewed By: ldionne, Mordante, #libc
Spies: libcxx-commits
Differential Revision: https://reviews.llvm.org/D130854
2022-07-31 21:54:25 +02:00
|
|
|
struct _HasToAddress<_Pointer, decltype((void)pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>())) >
|
2022-08-04 10:57:58 -07:00
|
|
|
: true_type {};
|
|
|
|
|
|
|
|
template <class _Pointer, class = void>
|
|
|
|
struct _HasArrow : false_type {};
|
|
|
|
|
|
|
|
template <class _Pointer>
|
[libc++][NFC] Qualify declval
While it's not necessary to qualify calls to `declval` it makes error messages very crypric if the declaration isn't reachable anymore
For example:
```
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:53:66: error: no type named 'type' in 'std::common_type<long, long>'
typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__type_traits/common_type.h:107:14: note: in instantiation of template class 'std::common_type<std::chrono::duration<long, std::ratio<3600, 1>>, std::chrono::duration<long, std::ratio<3600, 1>>>' requested here
: public common_type<_Tp, _Tp> {};
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:279:58: note: in instantiation of template class 'std::common_type<std::chrono::duration<long, std::ratio<3600, 1>>>' requested here
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator+() const {return typename common_type<duration>::type(*this);}
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:308:54: note: in instantiation of template class 'std::chrono::duration<long, std::ratio<3600, 1>>' requested here
typedef duration< int, ratio_multiply<ratio<24>, hours::period>> days;
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:280:81: error: no type named 'type' in 'std::common_type<std::chrono::duration<long, std::ratio<3600, 1>>>'
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator-() const {return typename common_type<duration>::type(-__rep_);}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:308:54: note: in instantiation of template class 'std::chrono::duration<long, std::ratio<3600, 1>>' requested here
typedef duration< int, ratio_multiply<ratio<24>, hours::period>> days;
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:53:66: error: no type named 'type' in 'std::common_type<int, int>'
typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__type_traits/common_type.h:107:14: note: in instantiation of template class 'std::common_type<std::chrono::duration<int, std::ratio<86400, 1>>, std::chrono::duration<int, std::ratio<86400, 1>>>' requested here
: public common_type<_Tp, _Tp> {};
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:279:58: note: in instantiation of template class 'std::common_type<std::chrono::duration<int, std::ratio<86400, 1>>>' requested here
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator+() const {return typename common_type<duration>::type(*this);}
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:309:55: note: in instantiation of template class 'std::chrono::duration<int, std::ratio<86400, 1>>' requested here
typedef duration< int, ratio_multiply<ratio<7>, days::period>> weeks;
^
19 similar errors omitted
```
changes with qualification added to:
```
While building module 'std' imported from /home/nikolask/llvm-projects/libcxx/libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/common_type.pass.cpp:13:
In file included from <module-includes>:17:
In file included from /home/nikolask/llvm-projects/libcxx/build/include/c++/v1/math.h:309:
In file included from /home/nikolask/llvm-projects/libcxx/build/include/c++/v1/limits:107:
In file included from /home/nikolask/llvm-projects/libcxx/build/include/c++/v1/type_traits:432:
In file included from /home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__type_traits/common_reference.h:13:
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__type_traits/common_type.h:28:43: error: declaration of 'declval' must be imported from module 'std.utility.__utility.declval' before it is required
using __cond_type = decltype(false ? std::declval<_Tp>() : std::declval<_Up>());
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__utility/declval.h:30:34: note: declaration here is not visible
decltype(std::__declval<_Tp>(0)) declval() _NOEXCEPT;
^
/home/nikolask/llvm-projects/libcxx/libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/common_type.pass.cpp:13:10: fatal error: could not build module 'std'
#include <functional>
~~~~~~~~^
2 errors generated.
```
Reviewed By: ldionne, Mordante, #libc
Spies: libcxx-commits
Differential Revision: https://reviews.llvm.org/D130854
2022-07-31 21:54:25 +02:00
|
|
|
struct _HasArrow<_Pointer, decltype((void)std::declval<const _Pointer&>().operator->()) > : true_type {};
|
2022-08-04 10:57:58 -07:00
|
|
|
|
|
|
|
template <class _Pointer>
|
|
|
|
struct _IsFancyPointer {
|
|
|
|
static const bool value = _HasArrow<_Pointer>::value || _HasToAddress<_Pointer>::value;
|
|
|
|
};
|
|
|
|
|
2021-05-04 18:51:58 -04:00
|
|
|
// enable_if is needed here to avoid instantiating checks for fancy pointers on raw pointers
|
2022-08-04 10:57:58 -07:00
|
|
|
template <class _Pointer, class = __enable_if_t< _And<is_class<_Pointer>, _IsFancyPointer<_Pointer> >::value > >
|
2021-05-04 18:51:58 -04:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
|
2023-03-27 18:22:23 +02:00
|
|
|
__decay_t<decltype(__to_address_helper<_Pointer>::__call(std::declval<const _Pointer&>()))>
|
2021-05-04 18:51:58 -04:00
|
|
|
__to_address(const _Pointer& __p) _NOEXCEPT {
|
|
|
|
return __to_address_helper<_Pointer>::__call(__p);
|
2021-01-15 12:59:56 -05:00
|
|
|
}
|
|
|
|
|
2021-05-04 18:51:58 -04:00
|
|
|
template <class _Pointer, class>
|
|
|
|
struct __to_address_helper {
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static decltype(std::__to_address(
|
[libc++][NFC] Qualify declval
While it's not necessary to qualify calls to `declval` it makes error messages very crypric if the declaration isn't reachable anymore
For example:
```
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:53:66: error: no type named 'type' in 'std::common_type<long, long>'
typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__type_traits/common_type.h:107:14: note: in instantiation of template class 'std::common_type<std::chrono::duration<long, std::ratio<3600, 1>>, std::chrono::duration<long, std::ratio<3600, 1>>>' requested here
: public common_type<_Tp, _Tp> {};
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:279:58: note: in instantiation of template class 'std::common_type<std::chrono::duration<long, std::ratio<3600, 1>>>' requested here
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator+() const {return typename common_type<duration>::type(*this);}
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:308:54: note: in instantiation of template class 'std::chrono::duration<long, std::ratio<3600, 1>>' requested here
typedef duration< int, ratio_multiply<ratio<24>, hours::period>> days;
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:280:81: error: no type named 'type' in 'std::common_type<std::chrono::duration<long, std::ratio<3600, 1>>>'
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator-() const {return typename common_type<duration>::type(-__rep_);}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:308:54: note: in instantiation of template class 'std::chrono::duration<long, std::ratio<3600, 1>>' requested here
typedef duration< int, ratio_multiply<ratio<24>, hours::period>> days;
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:53:66: error: no type named 'type' in 'std::common_type<int, int>'
typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__type_traits/common_type.h:107:14: note: in instantiation of template class 'std::common_type<std::chrono::duration<int, std::ratio<86400, 1>>, std::chrono::duration<int, std::ratio<86400, 1>>>' requested here
: public common_type<_Tp, _Tp> {};
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:279:58: note: in instantiation of template class 'std::common_type<std::chrono::duration<int, std::ratio<86400, 1>>>' requested here
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator+() const {return typename common_type<duration>::type(*this);}
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:309:55: note: in instantiation of template class 'std::chrono::duration<int, std::ratio<86400, 1>>' requested here
typedef duration< int, ratio_multiply<ratio<7>, days::period>> weeks;
^
19 similar errors omitted
```
changes with qualification added to:
```
While building module 'std' imported from /home/nikolask/llvm-projects/libcxx/libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/common_type.pass.cpp:13:
In file included from <module-includes>:17:
In file included from /home/nikolask/llvm-projects/libcxx/build/include/c++/v1/math.h:309:
In file included from /home/nikolask/llvm-projects/libcxx/build/include/c++/v1/limits:107:
In file included from /home/nikolask/llvm-projects/libcxx/build/include/c++/v1/type_traits:432:
In file included from /home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__type_traits/common_reference.h:13:
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__type_traits/common_type.h:28:43: error: declaration of 'declval' must be imported from module 'std.utility.__utility.declval' before it is required
using __cond_type = decltype(false ? std::declval<_Tp>() : std::declval<_Up>());
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__utility/declval.h:30:34: note: declaration here is not visible
decltype(std::__declval<_Tp>(0)) declval() _NOEXCEPT;
^
/home/nikolask/llvm-projects/libcxx/libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/common_type.pass.cpp:13:10: fatal error: could not build module 'std'
#include <functional>
~~~~~~~~^
2 errors generated.
```
Reviewed By: ldionne, Mordante, #libc
Spies: libcxx-commits
Differential Revision: https://reviews.llvm.org/D130854
2022-07-31 21:54:25 +02:00
|
|
|
std::declval<const _Pointer&>().operator->()))
|
2021-08-31 14:29:24 -04:00
|
|
|
__call(const _Pointer& __p) _NOEXCEPT {
|
2021-05-04 18:51:58 -04:00
|
|
|
return std::__to_address(__p.operator->());
|
|
|
|
}
|
2021-01-15 12:59:56 -05:00
|
|
|
};
|
|
|
|
|
2021-05-04 18:51:58 -04:00
|
|
|
template <class _Pointer>
|
[libc++][NFC] Qualify declval
While it's not necessary to qualify calls to `declval` it makes error messages very crypric if the declaration isn't reachable anymore
For example:
```
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:53:66: error: no type named 'type' in 'std::common_type<long, long>'
typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__type_traits/common_type.h:107:14: note: in instantiation of template class 'std::common_type<std::chrono::duration<long, std::ratio<3600, 1>>, std::chrono::duration<long, std::ratio<3600, 1>>>' requested here
: public common_type<_Tp, _Tp> {};
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:279:58: note: in instantiation of template class 'std::common_type<std::chrono::duration<long, std::ratio<3600, 1>>>' requested here
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator+() const {return typename common_type<duration>::type(*this);}
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:308:54: note: in instantiation of template class 'std::chrono::duration<long, std::ratio<3600, 1>>' requested here
typedef duration< int, ratio_multiply<ratio<24>, hours::period>> days;
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:280:81: error: no type named 'type' in 'std::common_type<std::chrono::duration<long, std::ratio<3600, 1>>>'
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator-() const {return typename common_type<duration>::type(-__rep_);}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:308:54: note: in instantiation of template class 'std::chrono::duration<long, std::ratio<3600, 1>>' requested here
typedef duration< int, ratio_multiply<ratio<24>, hours::period>> days;
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:53:66: error: no type named 'type' in 'std::common_type<int, int>'
typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__type_traits/common_type.h:107:14: note: in instantiation of template class 'std::common_type<std::chrono::duration<int, std::ratio<86400, 1>>, std::chrono::duration<int, std::ratio<86400, 1>>>' requested here
: public common_type<_Tp, _Tp> {};
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:279:58: note: in instantiation of template class 'std::common_type<std::chrono::duration<int, std::ratio<86400, 1>>>' requested here
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator+() const {return typename common_type<duration>::type(*this);}
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:309:55: note: in instantiation of template class 'std::chrono::duration<int, std::ratio<86400, 1>>' requested here
typedef duration< int, ratio_multiply<ratio<7>, days::period>> weeks;
^
19 similar errors omitted
```
changes with qualification added to:
```
While building module 'std' imported from /home/nikolask/llvm-projects/libcxx/libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/common_type.pass.cpp:13:
In file included from <module-includes>:17:
In file included from /home/nikolask/llvm-projects/libcxx/build/include/c++/v1/math.h:309:
In file included from /home/nikolask/llvm-projects/libcxx/build/include/c++/v1/limits:107:
In file included from /home/nikolask/llvm-projects/libcxx/build/include/c++/v1/type_traits:432:
In file included from /home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__type_traits/common_reference.h:13:
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__type_traits/common_type.h:28:43: error: declaration of 'declval' must be imported from module 'std.utility.__utility.declval' before it is required
using __cond_type = decltype(false ? std::declval<_Tp>() : std::declval<_Up>());
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__utility/declval.h:30:34: note: declaration here is not visible
decltype(std::__declval<_Tp>(0)) declval() _NOEXCEPT;
^
/home/nikolask/llvm-projects/libcxx/libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/common_type.pass.cpp:13:10: fatal error: could not build module 'std'
#include <functional>
~~~~~~~~^
2 errors generated.
```
Reviewed By: ldionne, Mordante, #libc
Spies: libcxx-commits
Differential Revision: https://reviews.llvm.org/D130854
2022-07-31 21:54:25 +02:00
|
|
|
struct __to_address_helper<_Pointer,
|
|
|
|
decltype((void)pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>()))> {
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static decltype(pointer_traits<_Pointer>::to_address(
|
|
|
|
std::declval<const _Pointer&>()))
|
2021-08-31 14:29:24 -04:00
|
|
|
__call(const _Pointer& __p) _NOEXCEPT {
|
2021-05-04 18:51:58 -04:00
|
|
|
return pointer_traits<_Pointer>::to_address(__p);
|
|
|
|
}
|
|
|
|
};
|
2021-05-04 18:48:16 -04:00
|
|
|
|
2023-02-14 00:56:09 +01:00
|
|
|
#if _LIBCPP_STD_VER >= 20
|
[libc++] Fix std::to_address(array).
There were basically two bugs here:
When C++20 `to_address` is called on `int arr[10]`, then `const _Ptr&` becomes
a reference to a const array, and then we dispatch to `__to_address<const int(&)[10]>`,
which, oops, gives us a `const int*` result instead of an `int*` result.
Solution: We need to provide the two standard-specified overloads of
`std::to_address` in exactly the same way that we provide two overloads
of `__to_address`.
When `__to_address` is called on a pointer type, `__to_address(const _Ptr&)`
is disabled so we successfully avoid trying to instantiate pointer_traits of
that pointer type. But when it's called on an array type, it's not disabled
for array types, so we go ahead and instantiate pointer_traits<int[10]>,
which goes boom. Solution: We need to disable `__to_address(const _Ptr&)`
for both pointer and array types. Also disable it for function types,
so that they get the nice error message; and put a test on it.
Differential Revision: https://reviews.llvm.org/D109331
2021-09-06 14:11:45 -04:00
|
|
|
template <class _Tp>
|
|
|
|
inline _LIBCPP_HIDE_FROM_ABI constexpr auto to_address(_Tp* __p) noexcept {
|
|
|
|
return std::__to_address(__p);
|
|
|
|
}
|
|
|
|
|
2021-01-15 12:59:56 -05:00
|
|
|
template <class _Pointer>
|
2022-08-04 10:57:58 -07:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI constexpr auto to_address(const _Pointer& __p) noexcept
|
|
|
|
-> decltype(std::__to_address(__p)) {
|
2021-01-15 12:59:56 -05:00
|
|
|
return std::__to_address(__p);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-12-10 18:28:13 -05:00
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
2021-04-20 12:03:32 -04:00
|
|
|
#endif // _LIBCPP___MEMORY_POINTER_TRAITS_H
|