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

1

8

Following https://mcmap.net/q/377885/-find-the-smallest-amongst-3-numbers-in-c-duplicate 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?

Stacy answered 15/6, 2017 at 8:1 Comment(4)
I take it that you actually mean C++11?Logician
#include <algorithm>? 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.Ferris
@Logician Yes, I have edited it. I am using CLion and CMakeLists includes the line: set(CMAKE_CXX_STANDARD 11)Stacy
@Ferris Thank you! That solved it =)Stacy
S
12

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.

Stacy answered 15/6, 2017 at 8:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.