lundi 15 février 2021

Does a range convert to bool via emtpy() member function when used as a if condition?

The following code compiles, runs, and gives the result as if range is being converted to bool via range.empty(). Is it actually what happens? I'm really not comfortable enough in navigating Range-v3 headers, so I'm asking this question here.

#include <iostream>
#include <range/v3/view/take.hpp>
#include <range/v3/view/drop.hpp>
#include <vector>

int main() {

    std::vector<int> v{1};
    if (auto range = v | ranges::views::take(1)) {
        std::cout << "take" << std::endl;
    }
    if (auto range = v | ranges::views::drop(1)) {
        std::cout << "drop" << std::endl;
    }
}

Aucun commentaire:

Enregistrer un commentaire