As stated in this answer a std::vector<T>
cannot contain const T
, or classes with const
-members. However, this is not the case when T = std::pair<const int, int>
, as shown below. Why is this the case? How is std::pair
special?
#include <utility>
#include <vector>
struct foo
{
const int first;
int second;
};
int main() {
std::vector<std::pair<const int, int>> V1;
V1.resize(3); // This compiles
std::vector<foo> V2;
V2.resize(3); // This gives the error listed below
}
error: use of deleted function 'foo::foo()'
note: 'foo::foo()' is implicitly deleted because the default definition would be ill-formed: