Boost logo

Boost :

From: George A. Heintzelman (georgeh_at_[hidden])
Date: 2001-10-09 07:46:53


> Hi,
>
> Is the following legal for regex?
>
> const char* data = ...;
> const size_t length = ...;
> boost::cmatch what;

I think this is not correct. It should be boost::match_results<std::stri
ng::const_iterator> in order to work with the below expression. We need
a typedef for this commonly used type. But that shouldn't affect your
later analysis.

> if( boost::regex_match( std::string( data, length ), what ) {
> ...
> cout << what[ 1 ] << endl;
> }
>
> The problem is that I pass a temporary object and AFAICS 'what' only
> stores a reference (iterator?) to the source. When accessing what[ 1 ],
> the data is no longer valid. Should this be documented (I haven't found
> anything in the docs) or should the implementation be fixed? It seems to
> me that this is a very dangerous interface as nothing in the above code
> looks 'suspicious'. Or am I missing something?

I think your analysis is basically correct. Note that it would be more
correct and more efficient to use:

boost::regex_match(data,data+length, what)

instead. (You probably knew that, but others might not have...) I do
think that a note should be put in the documentation that a
match_results object is valid only as long as the iterators to the
object originally matched are valid.

George Heintzelman
georgeh_at_[hidden]


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