Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does MSVC10 Visual Studio 2010 support C++ range based loops

Tags:

c++

c++11

Does MSVC10 support the C++0x draft standard's range based loop?

http://en.wikipedia.org/wiki/C%2B%2B0x#Range-based_for-loop

Example:

for (int& p : array) { ... }
like image 825
MW_dev Avatar asked Aug 01 '11 13:08

MW_dev


2 Answers

No. Stephan T. Lavavej's explains the feature was altered too late in Visual Studio 2010's release cycle.

Comments on the Visual Studio Team's blog: http://blogs.msdn.com/b/vcblog/archive/2009/07/13/intellisense-and-browsing-with-c-0x.aspx

like image 120
MW_dev Avatar answered Nov 04 '22 09:11

MW_dev


It doesn't, but you can simulate it with a macro.

template<typename T>
struct false_wrapper
{
    false_wrapper(const T& value) : value(value) { }

    operator bool() const { return false; }

    T value;
};

template<typename T>
false_wrapper<T> make_false_wrapper(const T& value)
{
    return false_wrapper<T>(value);
}

template<typename T>
struct false_ref_wrapper
{
    false_ref_wrapper(T& value) : value(value) { }

    operator bool() const { return false; }

    T& value;

private:
    false_ref_wrapper& operator=(const false_ref_wrapper&);
};

template<typename T>
false_ref_wrapper<T> make_false_ref_wrapper(T& value)
{
    return false_ref_wrapper<T>(value);
}

template<typename T>
void increment(T& it)
{
    ++it;
}

#define foreach_(VAL, VALS) \
    if (auto _foreach_col = make_false_ref_wrapper(VALS)) { } else \
    if (auto _foreach_cur = make_false_wrapper(std::begin(_foreach_col.value))) { } else \
    if (auto _foreach_end = make_false_wrapper(std::end(_foreach_col.value))) { } else \
    for (bool _foreach_flag = true; \
              _foreach_flag && _foreach_cur.value != _foreach_end.value; \
              _foreach_flag ? increment(_foreach_cur.value) : (void) 0) \
        if ((_foreach_flag = false) == true) { } else \
        for (VAL = *_foreach_cur.value; !_foreach_flag; _foreach_flag = true)
like image 24
Valentin Milea Avatar answered Nov 04 '22 08:11

Valentin Milea