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

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

Posted by: admin January 9, 2018 Leave a comment

Questions:

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) { ... }
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

Questions:
Answers:

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)