To help you get the point, I give my codes:(main.cpp),only one file involved.
#include <iostream>
#include <vector>
using namespace std;
class test{
public :
int member {0};
void fun(){cout << "member is " << member << endl;}
test(){}
//test(int param) : member(param){} //this line is commented.
};
int main()
{
vector<test> *vp = new vector<test>[2] {{10},{20}};
//vector<test> array[2] {{10},{20}};//this won't work either.
cout << "size of vp[0] is " << vp[0].size() << endl;
cout << "size of vp[1] is " << vp[1].size() << endl;
return 0;
}
I intend to initialize vp[0]
to size 10 and vp[1]
to size 20
. However when I compiled it on mac
using g++ -std=c++11 main.cpp -o main
it complained:
main.cpp:14:45: error: chosen constructor is explicit in copy-initialization
vector<test> *vp = new vector<test>[2] {{10},{20}};
^~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:517:14: note:
constructor declared here
explicit vector(size_type __n);
^
main.cpp:14:50: error: chosen constructor is explicit in copy-initialization
vector<test> *vp = new vector<test>[2] {{10},{20}};
^~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:517:14: note:
constructor declared here
explicit vector(size_type __n);
^
In CentOS Linux using the same command and I got
main.cpp: In function ‘int main()’:
main.cpp:14:54: error: converting to ‘std::vector<test>’ from initializer list would use explicit constructor ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const allocator_type&) [with _Tp = test; _Alloc = std::allocator<test>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<test>]’
vector<test> *vp = new vector<test>[2] {{10},{20}};
^
main.cpp:14:54: error: converting to ‘std::vector<test>’ from initializer list would use explicit constructor ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const allocator_type&) [with _Tp = test; _Alloc = std::allocator<test>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<test>]’
What is going on here? Why does it have anything to do with keyword explicit
?
I know vector have several constructors(such as the one with initializer-list type argument). If I initialize the vector like vector<test> temp {10}
, this would initialize vector to be of size 10 without any explicit
concerns. I don't know what is hidden inside when it comes to vector<test> array[2] {{10},{20}}
that cause me the bug.
Interestingly, if I provide class test
with a constructor with one argument(just uncomment the line in my code), the compiler does not complain at all. But the meaning of vector<test> array[2] {{10},{20}}
changed to initialize the vector array[0]
with 2 test
type objects initialized with 10
and 20
respectively. But the syntax vector<test> array[2] {10,20}
,which i tried later, is wrong again.
I don't know what is going on here and am totally lost. Isn't {10,20}
of initializer-list type too?
I really appreciate it if you can explain what's going on here, and how to initialize an array of vector of different size(please do not use circumventing ways). I want to know what does the syntax means exactly.