Saurabh Kumar
Saurabh Kumar

Reputation: 447

What is vector<int,int> Is it even valid?

I have been working with vector < pair < int,int>> but I didnt know that something like vector< int,int> also exists and I can declare it, but dont know how to use it.

Is vector < int,int> a valid container,if yes whats the difference between that and vector < pair < int,int>> ?

if no, why am I able to declare it??

Upvotes: 1

Views: 17020

Answers (2)

eerorika
eerorika

Reputation: 238491

What is vector<int,int>

The second tempalate argument of std::vector is allocator, so this is a vector, whose allocator is of type int.

Is it even valid?

No. int does not satisfy requirements of an allocator.

if no, why am I able to declare it??

Well, instantiating a template with at least two template arguments with at most two template arguments that do not have a default using two type arguments is well-formed. It's not until you try to use the allocator that you potentially get a problem with well-formedness.

There is no way of expressing in the language that a template argument must satisfy certain properties, and the standard library doesn't have such enforcement either. The Concepts feature proposed to be introduced in a future C++ standard can be used to enforce (some of) such requirements, and maybe the containers will also be required to use concepts in the future.

If your template instantiation doesn't meet the required specification of the standard container, the standard does not require the compiler to diagnose the error. Quoting latest standard draft:

[res.on.functions]

In certain cases (replacement functions, handler functions, operations on types used to instantiate standard library template components), the C++ standard library depends on components supplied by a C++ program. If these components do not meet their requirements, this document places no requirements on the implementation.

In particular, the effects are undefined in the following cases:

  • for types used as template arguments when instantiating a template component, if the operations on the type do not implement the semantics of the applicable Requirements subclause ([allocator.requirements], [container.requirements], [iterator.requirements], [algorithms.requirements], [numeric.requirements]). Operations on such types can report a failure by throwing an exception unless otherwise specified.

The declaration could in theory compile, as there is no restrictions. On my system, it does not compile.

Upvotes: 5

Ipiano
Ipiano

Reputation: 279

The key here is the list of template arguments in std::vector

According to the documentation @ https://devdocs.io/cpp/header/vector, std::vector takes two template arguments, the T (type of thing to store) and an Allocator, which is some type matching the Allocator concept.

template <class T, class Allocator = allocator<T> >
class vector

The allocator is used to, well, do allocations. If you pass an int as the allocator, it should fail to compile (And it definitely does for me) because int does not contain any of the functionality expected in an Allocator.

Your first example, vector<pair<int, int>> is a vector where the contained object is a pair of ints, and the Allocator used is the default one.

Upvotes: 3

Related Questions