std::function дает сбой при использовании в массиве в стеке

В MS Visual C++ 2010 SP1 этот код дает сбой:

#include "stdafx.h"

#include <functional>
#include <iostream>
//#include <vector>

int a = 0;

int _tmain(int argc, _TCHAR* argv[]) {
    // this way it works:
    //std::vector<std::function<void ()>> s;
    //s.push_back([]() { a = 1; });
    //s.push_back([]() { a = 2; int b = a; });

    std::function<void ()> s[] = { 
        []() { a = 1; },
        []() {
            a = 2;

            // Problem occurs only if the following line is included. When commented out no problem occurs.
            int b = a;
        }
    };

    int counter = 0;
    for (auto it = std::begin(s); it != std::end(s); ++it) {
        ++counter;
        (*it)();
        std::wcout << counter << L":" << a << std::endl;
    }

    return 0;
}

Когда создается второй элемент массива, он повреждает первый элемент массива.

Является ли это ошибкой компилятора или я сделал что-то, что не поддерживается стандартом C++ 11?

РЕДАКТИРОВАТЬ

Этот код работает в gcc-4.5.1:

#include <functional>
#include <iostream>
//#include <vector>

int a = 0;

int main(int argc, char* argv[]) {
    // this way it works:
    //std::vector<std::function<void ()>> s;
    //s.push_back([]() { a = 1; });
    //s.push_back([]() { a = 2; int b = a; });

    std::function<void ()> s[] = { 
        []() { a = 1; },
        []() {
            a = 2;

            // Problem occurs only if the following line is included. 
            //When commented out no problem occurs.
            int b = a;
        }
    };

    int counter = 0;
    ++counter;
    s[0]();
    std::wcout << counter << L":" << a << std::endl;
    ++counter;
    s[1]();
    std::wcout << counter << L":" << a << std::endl;

    return 0;
}
7
задан frast 1 April 2012 в 16:52
поделиться