Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c++ > #263

Re: Defect Report: Wordings on basic_string::data() disallow small-string optimization

From Daniel Krügler<daniel.kruegler@googlemail.com>
Newsgroups comp.std.c++
Subject Re: Defect Report: Wordings on basic_string::data() disallow small-string optimization
Date 2011-08-28 11:06 -0700
Organization A noiseless patient Spider
Message-ID <j3doq3$2p5$1@dont-email.me> (permalink)
References <4E58FF73.7070503@f2.dion.ne.jp>

Show all headers | View raw


Am 28.08.2011 17:42, schrieb Kazutoshi Satoda:
>  Daniel Kr�gler wrote:
>>
>>  On 2011-08-24 03:56, Kazutoshi Satoda wrote:
>>>
>>>  void f(std::string&  x, std::string&&  str)
>>>  {
>>>  char const* const was_pointed = str.data();
>>>  x = std::move(str);
>>>  assert(x.data() == was_pointed);
>>>  }
>
>  (snip)
>>
>>  You cannot make normative conclusions from this code, because it invokes
>>  undefined behaviour. This is so, because the expression
>>
>>  x = std::move(str)
>>
>>  calls a function that (potentially) invalidates the pointer returned
>>  fromstr.data(), ...
>
>  Then, replace the example with this one:
>
>  void f(std::string&  x, std::string&&  str)
>  {
>  char const* const was_pointed = str.data();
>  x = static_cast<std::string&&>(str);
>  assert(x.data() == was_pointed);
>  }

This variation does not change the situation. The problem was not the
call of std::move, but the call of the non-const member function (the
move assignment operator of std::string) in the expression

x = static_cast<std::string&&>(str);

The library specification does not require that the pointer obtained in
the previous line

char const* const was_pointed = str.data();

is still valid after the move-assignment. Thus, after the
move-assignement, the pointer obtained from str.data() is potentially
invalid. Note also, that the same restrictions apply to the member or
non-member swap function for basic_string. The footnote 234 in N3290
makes that undoubtedly clear:

"For example, as an argument to non-member functions swap() (21.4.8.8),
operator>>() (21.4.8.9), and getline() (21.4.8.9), or as an argument to
basic_string::swap()"

>>>  I think these wordings of effects on data() are unintentionally
>>>  over-constraining, and should be relaxed to keep the rationale for
>>>  C.2.11 "Allow small-string optimization" really valid.
>>
>>  I have not yet seen an example which proves this. Your above given
>>  example is invalid.
>
>  I believe the above revised example is now good to show the problem.

It is not.

HTH&  Greetings from Bremen,

- Daniel Krügler



-- 
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]

Back to comp.std.c++ | Previous | NextPrevious in thread | Find similar


Thread

Re: Defect Report: Wordings on basic_string::data() disallow small-string optimization Kazutoshi Satoda <k_satoda@f2.dion.ne.jp> - 2011-08-28 08:42 -0700
  Re: Defect Report: Wordings on basic_string::data() disallow small-string optimization Daniel Krügler<daniel.kruegler@googlemail.com> - 2011-08-28 11:06 -0700

csiph-web