Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

std

c++11

min

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?

like image 459
gentmatt Avatar asked Jun 15 '17 08:06

gentmatt


1 Answers

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.

like image 99
gentmatt Avatar answered Nov 15 '22 03:11

gentmatt