On Feb 14, 2014, at 4:31 PM, David Blaikie <[email protected]> wrote:
> On Fri, Feb 14, 2014 at 4:21 PM, Marshall Clow <[email protected]> wrote:
> Users can write
>         for(sregex_iterator i(s.begin(), s.end(), regex("meow")), end; i != 
> end; ++i)
> 
> binding a temporary regex to const regex& and storing a pointer to it.
> This will compile silently, triggering undefined behavior at runtime.
> 
> Fixing this involves defining constructors for the various regex iterator 
> types from rvalue regexes, and then marking them as “deleted”.
> And tests.
> 
> Would this break (what I assume is) correct code doing things like:
> 
> std::find(sregex_iterator(s.begin(), s.end(), regex("meow")), end, "foo”);  ?

(Recapping a discussion on IRC)

Yes, it would.
And though that code is "technically correct” it (a) is dangerous, and (b) 
doesn’t do anything useful.

The return result from std::find is an interator into a destroyed object, so 
you can’t really do anything with that.

The “fix” for this code is simple:

        regex re{“meow”}
        std::find(sregex_iterator(s.begin(), s.end(), re), end, "foo”); 

and now the return value of std::find is valid (until the end of the lifetime 
of “re”).

See 
        http://cplusplus.github.io/LWG/lwg-active.html#2329
and     http://cplusplus.github.io/LWG/lwg-active.html#2332

for the changes to the (C++14) standard.

— Marshall

_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to