BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslstl_istringstream

Detailed Description

Outline

Purpose

Provide a C++03-compatible istringstream class.

Classes

Canonical header: bsl_sstream.h

See also

Description

This component is for internal use only. Please include <bsl_sstream.h> instead.

This component defines a class template, bsl::basic_istringstream, implementing a standard input stream that provides a constructor (as well as a manipulator) allowing clients to set the sequence of characters from which input is streamed to a supplied bsl::basic_string (see 27.8.3 [istringstream] of the C++11 standard). This component also defines two standard aliases, bsl::istringstream and bsl::wistringstream, that refer to specializations of the bsl::basic_istringstream template for char and wchar_t types, respectively. The bsl::basic_istringstream template has three parameters, CHAR_TYPE, CHAR_TRAITS, and ALLOCATOR. The CHAR_TYPE and CHAR_TRAITS parameters respectively define the character type for the string-stream and a type providing a set of operations the string-stream will use to manipulate characters of that type, which must meet the character traits requirements defined by the C++11 standard, 21.2 [char.traits]. The ALLOCATOR template parameter is described in the "Memory Allocation" section below.

Memory Allocation

The type supplied as a string-stream's ALLOCATOR template parameter determines how that string-stream will allocate memory. The basic_istringstream template supports allocators meeting the requirements of the C++11 standard, 17.6.3.5 [allocator.requirements]; in addition, it supports scoped-allocators derived from the bslma::Allocator memory allocation protocol. Clients intending to use bslma-style allocators should use bsl::allocator, which provides a C++11 standard-compatible adapter for a bslma::Allocator object. Note that the standard aliases bsl::istringstream and bsl::wistringstream both use bsl::allocator.

bslma-Style Allocators

If the type supplied for the ALLOCATOR template parameter of an istringstream instantiation is bsl::allocator, then objects of that string-stream type will conform to the standard behavior of a bslma-allocator-enabled type. Such a string-stream accepts an optional bslma::Allocator argument at construction. If the address of a bslma::Allocator object is explicitly supplied at construction, it will be used to supply memory for the string-stream throughout its lifetime; otherwise, the string-stream will use the default allocator installed at the time of the string-stream's construction (see bslma_default ).

Usage

This section illustrates intended use of this component.

Example 1: Basic Input Operations

The following example demonstrates the use of bsl::istringstream to read data of various types from a bsl::string object.

Suppose we want to implement a simplified converter from bsl::string to a generic type, TYPE. We use bsl::istringstream to implement the fromString function. We initialize the input stream with the string passed as a parameter and then we read the data from the input stream with operator>>:

template <class TYPE>
TYPE fromString(const bsl::string& from)
{
TYPE val;
in >> val;
return val;
}
Definition bslstl_istringstream.h:176
Definition bslstl_string.h:1281

Finally, we verify that our fromString function works on some simple test cases:

assert(fromString<int>("1234") == 1234);
assert(fromString<short>("-5") == -5);
assert(fromString<bsl::string>("abc") == "abc");