8

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?

gentmatt
  • 343
  • 2
  • 12
  • 2
    I take it that you actually mean C++11? – Lundin Jun 15 '17 at 08:03
  • 8
    `#include `? As to why it works using the 2-arg version, I can only assume the implementation of the Standard Library you are using splits the algorithm header up in some way, and you are getting some transitive include through `iostream`, but this is certainly not guaranteed to work. – BoBTFish Jun 15 '17 at 08:03
  • @Lundin Yes, I have edited it. I am using CLion and CMakeLists includes the line: set(CMAKE_CXX_STANDARD 11) – gentmatt Jun 15 '17 at 08:05
  • 1
    @BoBTFish Thank you! That solved it =) – gentmatt Jun 15 '17 at 08:06

1 Answers1

11

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.

gentmatt
  • 343
  • 2
  • 12