2011-08-01 18 views
24

MSVC10, C++ 0x taslağı standardının aralık tabanlı döngüsünü destekliyor mu?MSVC10 Visual Studio 2010 C++ aralık tabanlı döngüleri destekliyor mu

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

Örnek:

for (int& p : array) { ... } 
+3

MSVC10 her (int & dizideki p) 'çalışmalıdır için bu söz @BenVoigt ne için dokümantasyon gibi –

+1

görünüyor' gibi bir şey önceki bir öneriyi hayata ] (http://msdn.microsoft.com/en-us/library/ms177203.aspx). Orijinal olarak C++/CLR kodu için tasarlanmış gibi görünüyor ve aynı zamanda CLR olmayan diziler ile çalışmayabilir gibi görünüyor. – crashmstr

+0

Bu for_each'in (t & t in tt) sadece VS2010 derleyicisinde çalışacağını farz ediyorum. – paulm

cevap

6

Gelmez, ancak bir makro ile taklit olabilir. [Nasıl yapılır:: her biri için STL topluluğu üzerinde yineleme

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) 
+4

Rahatsız etme, [BOOST_FOREACH] 'ı kullanın (http://www.boost.org/doc/libs/1_50_0/doc/html/foreach.html) – eudoxos

+7

@eudoxos: Her proje Boost'u karşılayamaz –