Also, why there is no obvious constructor from basic_string?
On 09/27/16 20:47, Daniel Krügler wrote:
> 2016-09-27 14:21 GMT+02:00 Andrey Semashev <andrey....@gmail.com>:
>> Hi,
>>
>> I'm looking at string_view interface definition in N4606 and wondering why
>> so many members are not marked noexcept? For example, these constructors:
>>
>> constexpr basic_string_view(const charT* str);
>> constexpr basic_string_view(const charT* str, size_type len);
>>
>> Assuming that the input string is not a constant expression, the first
>> constructor would call an equivalent of strlen(str), which in 99% is a
>> no-throw operation. Why not mark it
>> "noexcept(noexcept(traits::length(str))"? The second constructor does not
>> even need that and could be simply noexcept.
>>
>> I'm most interested in constructors, but other members like operator[],
>> front(), back(), substr(), compare() also don't seem to need to ever throw,
>> yet they are not marked noexcept.
>>
>> Is there a reason for these omissions?
>
> This is so, because these functions have a narrow contract. The
> current guideline for LWG was described by this proposal:
>
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3279.pdf
I see. Still, it feels wrong that functions that you never expect to
throw are not marked noexcept. When you write non-throwing code you
would look at the functions your code calls and not seeing noexcept
there immediately throws a red flag.
It is intentional that the function might start to throw in the future, or at least in certain compilation modes. A high quality implementation might well throw from front() called on an empty string_view when built in debug mode.
noexcept is properly reserved for functions that have no preconditions, or where throwing would never be an appropriate reaction to precondition violation.
HiIf there was a ctor from array reference, would it be noexcept?template<size_t N>constexpr basic_string_view(const charT(&str)[N]) noexcept;
--
You received this message because you are subscribed to a topic in the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this topic, visit https://groups.google.com/a/isocpp.org/d/topic/std-proposals/FaBXmXBWHHI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAGg_6%2BPv2BYcxMJ5aQ9uLb2qpgVZ3ThFjoWdKBNyZ4oNPm0A-A%40mail.gmail.com.
> > template<size_t N>
> > constexpr basic_string_view(const charT(&str)[N]) noexcept;
>
> It hasn't been proposed, has it?
>
> My guess would be no, since there is a precondition that str[N-1] == 0,
> giving it a narrow contract.
Why would it have such a precondition?
This constructor is unnecessary unless you want to differentiate between C-string literals and raw pointers (which is a breaking change BTW).
const char s[] = { 'H', 'e', 'l', 'l', 'o' };string_view b(s);
Right now:b.size() // UB
On Wednesday, 25 October 2017 18:51:43 PDT Nevin Liber wrote:
> Technically, it doesn't have to be a breaking change, as long as all it
>
> does is define behavior for:
> > const char s[] = { 'H', 'e', 'l', 'l', 'o' };
> > string_view b(s);
> >
> > Right now:
> >
> > b.size() // UB
>
> I don't see that as particularly useful.
QStringView has both a pointer and an array constructor. That means:
const char16_t str[] = u"Hello\0World";
QStringView(str).size() == 11 // constexpr
QStringView(+str).size() == 5 // not constexpr
If we had added the constructor at the moment the class was introduced, there
would have been no legacy to deal with. Now there will be, so it needs to be
done with great care.