Adding missing span deduction guides#891
Merged
JordanMaples merged 7 commits intoMay 29, 2020
Merged
Conversation
CaseyCarter
suggested changes
May 29, 2020
Comment on lines
+743
to
+747
| template <class Container> | ||
| span(Container&)->span<typename Container::value_type>; | ||
|
|
||
| template <class Container> | ||
| span(const Container&)->span<const typename Container::value_type>; |
Contributor
There was a problem hiding this comment.
If you only care about containers, this is fine. Non-containers sometimes have const-qualified elements even when they are themselves not const, which will break this. For example, in:
#include <gsl/span>
#include <string_view>
#include <type_traits>
using namespace std::literals;
int main() {
auto s = "Hello, World!"sv;
gsl::span x{s}; // Line 9
static_assert(std::is_same_v<decltype(x)::element_type, const char>);
}CTAD deduces span<char> on line 9, resulting in the compiler complaining that initialization from const char* is ill-formed.
I suggest instead:
Suggested change
| template <class Container> | |
| span(Container&)->span<typename Container::value_type>; | |
| template <class Container> | |
| span(const Container&)->span<const typename Container::value_type>; | |
| template <class Container, | |
| class Element = std::remove_pointer_t<decltype(std::declval<Container&>().data())>> | |
| span(Container&)->span<Element>; | |
| template <class Container, | |
| class Element = std::remove_pointer_t<decltype(std::declval<const Container&>().data())>> | |
| span(const Container&)->span<Element>; |
Contributor
There was a problem hiding this comment.
Okay, my OCD made me test these, so I'll submit a PR (JordanMaples#6) with this change and the updated test.
Add string_view test case and modify deduction guides
…ro unused' errors
…vector may not intend for type deduction.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I noticed while experimenting with std::span that some of the functionality wasn't 1:1 regarding CTAD.
The deduction of container types was missing. The following std::span example compiles without any problems, while the the gsl equivalent does not.
Ex: Current behavior
std::vector v{1,2,3,4}; std::span sp{v}; // no problem, compilesstd::vector v{1,2,3,4}; gsl::span sp{v}; // fails to compile. gsl::span<int>, however does compileBy examining the container's value_type in the deduction guide, we're able to deduce the internal type and construct the span. This more closely aligns gsl::span's behavior to std::span.