: ?
c_str() ; undefined
, . valueString
, c_str
. , : ,
, , .
( ) ,
string, :
std::string
getValue( std::string const& key )
{
return condition ? valueString : std:;string();
}
, .
value &mdash, ,
- - .
,
. , ,
; -,
, .
- :
std::string
getValue( std::string const& key, std::string const& ifNotFound )
{
return condition ? valueString : ifNotFound;
}
. , , ,
, .
The most common alternative is Fallibleeither Maybe
class: an object of a class that combines state (usually simple bool) and an instance of the actual data type. Whether the data is valid or independent of the status value, so you still need to check that:
Fallible<std::string>
getValue( std::string const& key )
{
return condition
? Fallible<std::string>( valueString )
: Fallible<std::string>();
}
This often works well inside too:
Fallible<std::string>
getValue( std::string const& key )
{
Fallible<std::string> results;
results.validate( valueString );
return results;
}
(Just an example of frequent and convenient patterns.)
source
share