I have compilers:
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4
Microsoft Visual Studio 2015 ver. 14.0. (Visual C++ 2015)
Source code:
#include <queue>
class C
{
};
class B
{
public:
// assignment and copy prohibited
B(const B&) = delete;
B& operator=(const B&) = delete;
B(int v1, int v2) : m_V1(v1), m_V2(v2) {}
private:
int m_V1;
int m_V2;
std::queue<C> m_Queue;
};
class A
{
public:
// assignment and copy prohibited
A(const A&) = delete;
A& operator=(const A&) = delete;
A(int p1, int p2) : arrB{ {p1+1, p2+2}, {p1+3, p2+4}, {p1+5, p2+6} } { }
private:
B arrB[3];
};
1) If I use g++ with
std::queue<C> m_Queue;
I get the following error:
make
g++ -std=c++11 -c test.cpp
test.cpp: In constructor ‘A::A(int, int)’:
test.cpp:29:70: error: use of deleted function ‘B::B(const B&)’
A(int p1, int p2) : arrB{ {p1+1, p2+2}, {p1+3, p2+4}, {p1+5, p2+6} } { }
^
test.cpp:11:3: error: declared here
B(const B&) = delete;
^
make: *** [test.o] Error 1
2) If I use g++ and comment the line
//std::queue<C> m_Queue;
or I use both variants (commented and not) with Microsoft Visual Studio 2015 I don't get any errors.
Why?
Update:
List-initialization uses direct-list-initialization or copy-list-initialization.
8.5.4 List-initialization
1 List-initialization is initialization of an object or reference from a braced-init-list. Such an initializer is called an initializer list, and the comma-separated initializer-clauses of the list are called the elements of the initializer list. An initializer list may be empty. List-initialization can occur in direct-initialization or copyinitialization contexts; list-initialization in a direct-initialization context is called direct-list-initialization and list-initialization in a copy-initialization context is called copy-list-initialization.
But I do not understand what sort of initialization to be used in my case. I supposed that should be used direct-list-initialization in accordance with http://en.cppreference.com/w/cpp/language/list_initialization
Update 2:
8.5.1 Aggregates [dcl.init.aggr]
1 An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).When an aggregate is initialized by an initializer list, as specified in 8.5.4, the elements of the initializer list are taken as initializers for the members of the aggregate, in increasing subscript or member order. Each member is copy-initialized from the corresponding initializer-clause.
An aggregate is an array and Each member is copy-initialized - answer?
But this does not explain why compiles without errors when commenting on the line:
//std::queue<C> m_Queue;