Skip to content

Commit

Permalink
Fix span iterator deduction guide (#61, thanks @Ryan-rsm-McKenzie)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmoene committed Nov 1, 2020
1 parent 81f7b15 commit 66be7b3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ span<>: Allows to construct from a non-null pointer to const and a size
span<>: Allows to construct from a temporary pointer and a size
span<>: Allows to construct from a temporary pointer to const and a size
span<>: Allows to construct from any pointer and a zero size
span<>: Allows to construct from an iterator and a size via an iterator deduction guide (C++17)
span<>: Allows to construct from a C-array
span<>: Allows to construct from a const C-array
span<>: Allows to construct from a C-array with size via decay to pointer (potentially dangerous)
Expand Down
6 changes: 4 additions & 2 deletions include/nonstd/span.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,10 @@ using nonstd::byte;

namespace std20 {

#if span_HAVE( DEDUCTION_GUIDES )
template< class T >
struct iter_reference { typedef T type; };
using iter_reference_t = decltype( *std::declval<T&>() );
#endif

} // namespace std20

Expand Down Expand Up @@ -1268,7 +1270,7 @@ span( Container const & ) -> span<const typename Container::value_type>;
// iterator: constraints: It satisfies contiguous_­iterator.

template< class It, class EndOrSize >
span( It, EndOrSize ) -> span< typename std11::remove_reference< typename std20::iter_reference<It>::type >::type >;
span( It, EndOrSize ) -> span< typename std11::remove_reference< typename std20::iter_reference_t<It> >::type >;

#endif // span_HAVE( DEDUCTION_GUIDES )

Expand Down
14 changes: 14 additions & 0 deletions test/span.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,20 @@ CASE( "span<>: Allows to construct from any pointer and a zero size" )
EXPECT_NO_THROW( F::nonnull() );
}

CASE( "span<>: Allows to construct from an iterator and a size via an iterator deduction guide (C++17)" )
{
#if span_HAVE( DEDUCTION_GUIDES )
char const * argv[] = { "prog", "arg1", "arg2" };
int const argc = DIMENSION_OF( argv );

span args(argv, argc);

EXPECT( span.length() == DIMENSION_OF( argv ) );
#else
EXPECT( !!"iterator deduction guide is not available (no C++17)" );
#endif
}

CASE( "span<>: Allows to construct from a C-array" )
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
Expand Down

0 comments on commit 66be7b3

Please sign in to comment.