Created on 2007-09-22.00:00:00 last changed 171 months ago
Proposed resolution:
Provide additional overloads for regex_replace(): one additional overload of the iterator-based form (taking const charT* fmt), and three additional overloads of the convenience form (one taking const charT* str, another taking const charT* fmt, and the third taking both const charT* str and const charT* fmt). [re.alg.replace]:
template <class OutputIterator, class BidirectionalIterator, class traits, class charT> OutputIterator regex_replace(OutputIterator out, BidirectionalIterator first, BidirectionalIterator last, const basic_regex<charT, traits>& e, const basic_string<charT>& fmt, regex_constants::match_flag_type flags = regex_constants::match_default); template <class OutputIterator, class BidirectionalIterator, class traits, class charT> OutputIterator regex_replace(OutputIterator out, BidirectionalIterator first, BidirectionalIterator last, const basic_regex<charT, traits>& e, const charT* fmt, regex_constants::match_flag_type flags = regex_constants::match_default);...
template <class traits, class charT> basic_string<charT> regex_replace(const basic_string<charT>& s, const basic_regex<charT, traits>& e, const basic_string<charT>& fmt, regex_constants::match_flag_type flags = regex_constants::match_default); template <class traits, class charT> basic_string<charT> regex_replace(const basic_string<charT>& s, const basic_regex<charT, traits>& e, const charT* fmt, regex_constants::match_flag_type flags = regex_constants::match_default); template <class traits, class charT> basic_string<charT> regex_replace(const charT* s, const basic_regex<charT, traits>& e, const basic_string<charT>& fmt, regex_constants::match_flag_type flags = regex_constants::match_default); template <class traits, class charT> basic_string<charT> regex_replace(const charT* s, const basic_regex<charT, traits>& e, const charT* fmt, regex_constants::match_flag_type flags = regex_constants::match_default);
Rationale:
Solved by 727.
[ 2010-01-27 Moved to Tentatively NAD after 5 positive votes on c++std-lib. Rationale added below. ]
[ 2009-10 Santa Cruz: ]
Leave Open. Though we believe this is solved by the proposed resolution to 727.
[ 2009-07 Frankfurt: ]
Daniel to tweak for us.
[ Sophia Antipolis: ]
We note that Boost already has these overloads. However, the proposed wording is provided only for [re.alg.replace]; wording is needed for the synopsis as well. We also note that this has impact on match_results::format, which may require further overloads.
Two overloads of regex_replace() are currently provided:
template <class OutputIterator, class BidirectionalIterator, class traits, class charT> OutputIterator regex_replace(OutputIterator out, BidirectionalIterator first, BidirectionalIterator last, const basic_regex<charT, traits>& e, const basic_string<charT>& fmt, regex_constants::match_flag_type flags = regex_constants::match_default); template <class traits, class charT> basic_string<charT> regex_replace(const basic_string<charT>& s, const basic_regex<charT, traits>& e, const basic_string<charT>& fmt, regex_constants::match_flag_type flags = regex_constants::match_default);
The absence of const charT * overloads prevents ordinary-looking code from compiling, such as:
const string s("kitten"); const regex r("en"); cout << regex_replace(s, r, "y") << endl;
The compiler error message will be something like "could not deduce template argument for 'const std::basic_string<_Elem> &' from 'const char[1]'".
Users expect that anything taking a basic_string<charT> can also take a const charT *. In their own code, when they write a function taking std::string (or std::wstring), they can pass a const char * (or const wchar_t *), thanks to basic_string's implicit constructor. Because the regex algorithms are templated on charT, they can't rely on basic_string's implicit constructor (as the compiler error message indicates, template argument deduction fails first).
If a user figures out what the compiler error message means, workarounds are available - but they are all verbose. Explicit template arguments could be given to regex_replace(), allowing basic_string's implicit constructor to be invoked - but charT is the last template argument, not the first, so this would be extremely verbose. Therefore, constructing a basic_string from each C string is the simplest workaround.
History | |||
---|---|---|---|
Date | User | Action | Args |
2010-10-21 18:28:33 | admin | set | messages: + msg3560 |
2010-10-21 18:28:33 | admin | set | messages: + msg3559 |
2010-10-21 18:28:33 | admin | set | messages: + msg3558 |
2010-10-21 18:28:33 | admin | set | messages: + msg3557 |
2010-10-21 18:28:33 | admin | set | messages: + msg3556 |
2010-10-21 18:28:33 | admin | set | messages: + msg3555 |
2010-10-21 18:28:33 | admin | set | messages: + msg3554 |
2007-09-22 00:00:00 | admin | create |