Can range-v3 contains work on initializer list?

I am quite happy that range-v3 finally adds a contains algorithm. But I would like if I would not have to produce a container for it to work. Consider this example:

#include <range/v3/algorithm/contains.hpp>
enum class Status{
    OK, 
    TimedOut,
    TryAgain,
    PermanentFailure
};

bool f(Status s){
    std::array can_retry{Status::OK, Status::TimedOut};
    return ranges::contains(can_retry,s);
}
int main(){
    return f(Status::TryAgain);
}

Here I would like to inline {Status::OK, Status::TimedOut} into call to contains, but it fails. Is there any way to do this?

P.S. please no comments about maintainability/readability of code I want, it is just an example code. :)

1 answer

  • answered 2020-02-12 23:18 N. Shead

    You should be able to inline your two-line snippet into one like so:

    return ranges::contains(std::array{Status::Ok, Status::TimedOut}, s);