In C++0x (ohh! read C++11), we have automatic type inference. One thing which made me curious was that I can’t create an array of auto variables. For example:
auto A[] = {1, 2, 3, 4}; // Error!
Any ideas why this might have been disallowed?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
autodeduces every brace-enclosed initializer list to astd::initializer_list<T>. (See §7.1.6.4.6 including the example).Unfortunately you cannot initialize an array or even
std::arrayfrom astd::initializer_listonce you have obtained it, but you can use astd::vector.Of course this defeats the whole purpose of what you are trying to do.
I tried writing a work around called
make_arraybut had to realize that this cannot ever work as the size of aninitializer_listisn’t part of its template arguments and so you only instantiate onemake_arraytemplate for eachT. This sucks.Well, apparently you can go for the variadic-template hack mentioned here How do I initialize a member array with an initializer_list?