As suspected, initializing std::pair
with curly braces doesn't work because std::pair
is not an aggregate.
std::pair <int, int> p = {1,2}; // Doesn't work
However, initializing an array of std::pair
works well (with a warning in gcc 4.9
)
std::pair <int, int> a_p[] = {
{1,2},
{3,4},
{5,6}
}; // Works fine
Why is this happening?
EDIT: This question has been marked as a possible duplicate of C++11 aggregate initialization for classes with non-static member initializers
However, this question does not talk about non-static-member-initializers, AFAIK, std::pair
has a user defined constructor.
Quoting from http://en.cppreference.com/w/cpp/header/utility,
template <class T1, class T2>
struct pair {
typedef T1 first_type;
typedef T2 second_type;
T1 first;
T2 second;
pair(const pair&) = default;
pair(pair&&) = default;
constexpr pair();
pair(const T1& x, const T2& y);
template<class U, class V> pair(U&& x, V&& y);
template<class U, class V> pair(const pair<U, V>& p);
template<class U, class V> pair(pair<U, V>&& p);
template <class... Args1, class... Args2>
pair(piecewise_construct_t,
tuple<Args1...> first_args, tuple<Args2...> second_args);
pair& operator=(const pair& p);
template<class U, class V> pair& operator=(const pair<U, V>& p);
pair& operator=(pair&& p) noexcept(see below);
template<class U, class V> pair& operator=(pair<U, V>&& p);
void swap(pair& p) noexcept( noexcept(swap(first, p.first)) &&
noexcept(swap(second, p.second)) );
};