gentmatt
gentmatt

Reputation: 363

Error when using std::min "no matching function for call to ‘min(<brace-enclosed initializer list>)’"

Following https://stackoverflow.com/a/9424211/3368959 I am trying to compare three numbers:

#include <iostream>

int main() {

    std::cout << std::min({2,5,1}) << std::endl;
    return 0;
}

But the compiler gives me the error:

error: no matching function for call to ‘min(<brace-enclosed initializer list>)’

However, the code compiles just fine when using

std::min(std::min(2,5),1)

But the first way should work with the c++11 standard. What could I be doing wrong?

Upvotes: 8

Views: 11845

Answers (1)

gentmatt
gentmatt

Reputation: 363

As @BoBTFish suggested:

In order to use template <class T> T min (initializer_list<T> il) one needs to include <algorithm> as is mentioned here.

Upvotes: 12

Related Questions