Files
clang-p2996/libcxx/test/std/utilities/memory/pointer.traits/pointer_to.pass.cpp
Arthur O'Dwyer 85d4e29fd8 [libc++] Fix __wrap_iter to be a proper contiguous iterator.
Instead of overloading `__to_address`, let's specialize `pointer_traits`.
Function overloads need to be in scope at the point where they're called,
whereas template specializations do not. (User code can provide pointer_traits
specializations to be used by already-included library code, so obviously
`__wrap_iter` can do the same.)

`pointer_traits<__wrap_iter<It>>` cannot provide `pointer_to`, because
you generally cannot create a `__wrap_iter` without also knowing the
identity of the container into which you're trying to create an iterator.
I believe this is OK; contiguous iterators are required to provide
`to_address` but *not* necessarily `pointer_to`.

Differential Revision: https://reviews.llvm.org/D110198
2021-09-22 18:51:46 -04:00

54 lines
1.6 KiB
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
//
//===----------------------------------------------------------------------===//
// <memory>
// template <class T>
// struct pointer_traits<T*>
// {
// static pointer pointer_to(<details>); // constexpr in C++20
// ...
// };
#include <memory>
#include <cassert>
#include "test_macros.h"
TEST_CONSTEXPR_CXX20 bool test()
{
{
int i = 0;
static_assert(std::is_same<decltype(std::pointer_traits<int*>::pointer_to(i)), int*>::value, "");
assert(std::pointer_traits<int*>::pointer_to(i) == &i);
}
{
int i = 0;
static_assert(std::is_same<decltype(std::pointer_traits<const int*>::pointer_to(i)), const int*>::value, "");
assert(std::pointer_traits<const int*>::pointer_to(i) == &i);
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
{
// Check that pointer_traits<void*> is still well-formed, even though it has no pointer_to.
static_assert(std::is_same<std::pointer_traits<void*>::element_type, void>::value, "");
static_assert(std::is_same<std::pointer_traits<const void*>::element_type, const void>::value, "");
static_assert(std::is_same<std::pointer_traits<volatile void*>::element_type, volatile void>::value, "");
}
return 0;
}