How the new range-based for loop in C++17 helps Ranges TS?

后端 未结 2 446
野的像风
野的像风 2020-12-04 12:52

The committee changed the range-based for loop from:

  • C++11:

    {
       auto && __range = range_expression ; 
       for (auto __begin = begin_         
    
    
            
2条回答
  •  没有蜡笔的小新
    2020-12-04 13:25

    The new specification allows __begin and __end to be of different type, as long as __end can be compared to __begin for inequality. __end doesn't even need to be an iterator and can be a predicate. Here is a silly example with a struct defining begin and end members, the latter being a predicate instead of an iterator:

    #include 
    #include 
    
    // a struct to get the first word of a string
    
    struct FirstWord {
        std::string data;
    
        // declare a predicate to make ' ' a string ender
    
        struct EndOfString {
            bool operator()(std::string::iterator it) { return (*it) != '\0' && (*it) != ' '; }
        };
    
        std::string::iterator begin() { return data.begin(); }
        EndOfString end() { return EndOfString(); }
    };
    
    // declare the comparison operator
    
    bool operator!=(std::string::iterator it, FirstWord::EndOfString p) { return p(it); }
    
    // test
    
    int main() {
        for (auto c : {"Hello World !!!"})
            std::cout << c;
        std::cout << std::endl; // print "Hello World !!!"
    
        for (auto c : FirstWord{"Hello World !!!"}) // works with gcc with C++17 enabled
            std::cout << c;
        std::cout << std::endl; // print "Hello"
    }
    

提交回复
热议问题