IBM Support

Instantiating STL containers with const types

Question & Answer


Question

When I try compiling the following construct the compilation fails while processing the xmemory standard header file. Is this the correct behaviour? #include int foo() { std::vector v; } $ xlC t.cpp "/usr/vacpp/include/xmemory", line 120.23: 1540-0401 (S) The member "std ::allocator::address(const_reference) const" is already declared.

Answer

The behaviour is correct as, according to the ISO C++ Standard, creating containers of const objects is not allowed. Section 23.1:3 indicates that the type of objects stored in a container must meet the requirements of CopyConstructible (20.1.3) and Assignable types. Assignable is defined in Table 64 in the same section as an object that can be assigned value, which means that object cannot be a const.

On the other hand, Table 31 in section 20.1.5 indicates that type T in std::allocator<T> can be of any type. This obviously contradicts the previously referenced section 23.1:3. The C++ Standard Committee has determined that section 23.1:3 is correct, and that Table 31 is in fact wrong in the aforementioned assumption. The C++ standard library defect 274 has been created to correct this inconsistency in the next C++ Standard release.

Click below for a related FAQ which describes the same error using a different test case:

Related Information

[{"Product":{"code":"SSJT9L","label":"XL C\/C++"},"Business Unit":{"code":"BU054","label":"Systems w\/TPS"},"Component":"C\/C++ Runtime","Platform":[{"code":"PF002","label":"AIX"}],"Version":"6.0;7.0;8.0","Edition":"","Line of Business":{"code":"LOB08","label":"Cognitive Systems"}}]

Historical Number

06003

Document Information

Modified date:
08 August 2018

UID

swg21162689