Boost logo

Boost :

Subject: Re: [boost] [string_ref] string literal constructor
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2013-11-02 01:25:23


On Friday 01 November 2013 18:42:05 Michael Marcin wrote:
> I noticed string_ref doesn't have a constructor for a string literal.
> Wouldn't this save a call to strlen for a common case?
>
> Ex.
>
> template< std::size_t N >
> basic_string_ref( const charT( &str )[N] )
>
> : basic_string_ref( str, N-1 )
>
> {
> static_assert(N >= 1, "not a string literal");
> }
>
> string_ref test( "test" );
>
> I see that adding this directly doesn't work as the compiler is always
> choosing to decay and pick the const char* overload.
>
> Is there no way to make this work?

See boost/log/utility/string_literal.hpp, the above constructor is not quite
enough. It doesn't protect against constructing from an array of characters,
so for string_ref there shouldn't be such a constructor, it should always do
strlen.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk