Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't auto keyword working with initialization list of pointers to functions?

I've been recently trying out C++11, specifically the auto keyword, which turned out to be quiet interesting and useful.

However, for some reason it doesn't work with initialization list of pointers to functions. Firstly I looked for the answer in my book and I found that "auto keyword only works for single initialization values and not for initialization lists", but when I've tried:

auto foo = { "bar", "bar", "bar" };
std::cout << *(foo.begin());

it worked just fine, but when I try to do that:

// bar is a simple void bar() function
auto foo = { bar, bar, bar };

Visual Studio compiler spits:

error C1001: An internal error has occurred in the compiler.

While that works:

auto foo = bar;

Therefore I have decided to make a little trip over the internet to get more information about std::initializer_list and there I have learned that it's actually quite similar to std::vector. So, why wouldn't I try it?

std::vector<void (*)()> foo = { bar, bar, bar };
(*foo.begin())();

Work flawlessly. Since then I'm stuck and I have no idea why auto keyword doesn't work with initialization list of pointers to functions. Why does it have problems particularly with pointers to functions and what is most important does C++ standard say anything about it?

EDIT:

I've also tried the same thing using GCC and yup, it works and therefore it looks like you guys are right that MSVC compiler has a bug. I guess I'll need to wait until they fix it and in meantime I'll be simply using GCC.

like image 894
Peter Nimroot Avatar asked Dec 28 '25 20:12

Peter Nimroot


1 Answers

It seems that it is simply a bug of MS VC++. At least this code

auto foo = { bar, bar, bar };

where bar is some function is compiled successfully with GCC.

like image 53
Vlad from Moscow Avatar answered Dec 30 '25 22:12

Vlad from Moscow