fn() lexicalCast
Cast from a String-type to a numerical type

Defined in <seqan/stream.h>
Signature TTarget lexicalCast<TTarget>(source);


source The string to be read from. Type: SequenceConcept.

Template Parameters

TTarget Target type to cast to.

Return Values

TTarget Value of Type TTarget with cast contents of source.

Detailed Description

Return value undefined if casting fails, see lexicalCast2 for a more robust variant.

This function uses std::istringstream internally, so right now "123foobar" will be succesfully cast to an int of 123.


Using lexicalCast<>() is easy but not as robust as lexicalCast2: We cannot detect parsing or conversion errors.

unsigned u = 0;
int i = 0;
double = 0;
bool success = false;
u = lexicalCast<unsigned>( "3");   // => u is 3.
u = lexicalCast<unsigned>("-3");   // => u is undefined.
i = lexicalCast<int>("-3");        // => i is -3.
d = lexicalCast<double>("-3.99");  // => d is -3.99.

See Also