Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[libc++] Fix __segmented_iterator_traits for implicit template instantiation in SFINAE #134304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions libcxx/include/__iterator/segmented_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,22 @@
_LIBCPP_BEGIN_NAMESPACE_STD

template <class _Iterator>
struct __segmented_iterator_traits;
/* exposition-only:
{
using __segment_iterator = ...;
using __local_iterator = ...;
struct __segmented_iterator_traits {
using __is_segmented_iterator _LIBCPP_NODEBUG = false_type;
using __segment_iterator _LIBCPP_NODEBUG = void;
using __local_iterator _LIBCPP_NODEBUG = void;

/* exposition-only:
static __segment_iterator __segment(_Iterator);
static __local_iterator __local(_Iterator);
static __local_iterator __begin(__segment_iterator);
static __local_iterator __end(__segment_iterator);
static _Iterator __compose(__segment_iterator, __local_iterator);
*/
};
*/

template <class _Tp, size_t = 0>
struct __has_specialization : false_type {};

template <class _Tp>
struct __has_specialization<_Tp, sizeof(_Tp) * 0> : true_type {};

template <class _Iterator>
using __is_segmented_iterator _LIBCPP_NODEBUG = __has_specialization<__segmented_iterator_traits<_Iterator> >;
struct __is_segmented_iterator : __segmented_iterator_traits<_Iterator>::__is_segmented_iterator {};

_LIBCPP_END_NAMESPACE_STD

Expand Down
2 changes: 2 additions & 0 deletions libcxx/include/__ranges/join_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <__ranges/range_adaptor.h>
#include <__ranges/view_interface.h>
#include <__type_traits/common_type.h>
#include <__type_traits/integral_constant.h>
#include <__type_traits/maybe_const.h>
#include <__utility/as_lvalue.h>
#include <__utility/empty.h>
Expand Down Expand Up @@ -378,6 +379,7 @@ template <class _JoinViewIterator>
__has_random_access_iterator_category<typename _JoinViewIterator::_Outer>::value &&
__has_random_access_iterator_category<typename _JoinViewIterator::_Inner>::value)
struct __segmented_iterator_traits<_JoinViewIterator> {
using __is_segmented_iterator _LIBCPP_NODEBUG = true_type;
using __segment_iterator _LIBCPP_NODEBUG =
__iterator_with_data<typename _JoinViewIterator::_Outer, typename _JoinViewIterator::_Parent*>;
using __local_iterator _LIBCPP_NODEBUG = typename _JoinViewIterator::_Inner;
Expand Down
52 changes: 52 additions & 0 deletions libcxx/test/libcxx/iterators/segmented_iterator.pass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//

// <iterator>

// __segmented_iterator_traits<_Iter>

// verifies that __segmented_iterator_traits<_Iter> does not result in implicit
// template instantaition, which may cause hard errors in SFINAE.

// XFAIL: FROZEN-CXX03-HEADERS-FIXME

#include <array>
#include <deque>
#include <list>
#include <ranges>
#include <vector>
#include <__iterator/segmented_iterator.h>
#include <__type_traits/integral_constant.h>

#include "test_iterators.h"
#include "test_macros.h"

template <class Iter>
struct is_segmented_random_access_iterator
: std::_BoolConstant<std::__is_segmented_iterator<Iter>::value &&
std::__has_random_access_iterator_category<
typename std::__segmented_iterator_traits<Iter>::__local_iterator>::value> {};

int main(int, char**) {
static_assert(is_segmented_random_access_iterator<std::deque<int>::iterator>::value, "");
static_assert(!is_segmented_random_access_iterator<std::vector<int>::iterator>::value, "");
static_assert(!is_segmented_random_access_iterator<std::list<int>::iterator>::value, "");
static_assert(!is_segmented_random_access_iterator<std::array<int, 0>::iterator>::value, "");
static_assert(!is_segmented_random_access_iterator<cpp17_input_iterator<int*> >::value, "");
static_assert(!is_segmented_random_access_iterator<forward_iterator<int*> >::value, "");
static_assert(!is_segmented_random_access_iterator<random_access_iterator<int*> >::value, "");
static_assert(!is_segmented_random_access_iterator<int*>::value, "");

#if TEST_STD_VER >= 20
using join_view_iterator = decltype((std::declval<std::vector<std::vector<int > >&>() | std::views::join).begin());
static_assert(is_segmented_random_access_iterator<join_view_iterator>::value, "");
#endif

return 0;
}