Skip to content

Instantly share code, notes, and snippets.

@dkorolev
Created December 20, 2014 19:52
Show Gist options
  • Save dkorolev/8c79ac6dbde8ce1d93e1 to your computer and use it in GitHub Desktop.
Save dkorolev/8c79ac6dbde8ce1d93e1 to your computer and use it in GitHub Desktop.
Learned today that C++11 does not support overloading by lambda type.

c++11-lambda-type.cc

#include <functional>

void foo(std::function<void()>) {}
void foo(std::function<bool()>) {}

int main() {
  foo([]() -> bool { return true; });
}

g++ -std=c++11 c++11-lambda-type.cc

c++11-lambda-type.cc: In function ‘int main()’:
c++11-lambda-type.cc:7:36: error: call of overloaded ‘foo(main()::__lambda0)’ is ambiguous
   foo([]() -> bool { return true; });
                                    ^
c++11-lambda-type.cc:7:36: note: candidates are:
c++11-lambda-type.cc:3:6: note: void foo(std::function<void()>)
 void foo(std::function<void()>) {}
      ^
c++11-lambda-type.cc:4:6: note: void foo(std::function<bool()>)
 void foo(std::function<bool()>) {}
      ^

clang++ -std=c++11 c++11-lambda-type.cc

c++11-lambda-type.cc:7:3: error: call to 'foo' is ambiguous
  foo([]() -> bool { return true; });
  ^~~
c++11-lambda-type.cc:3:6: note: candidate function
void foo(std::function<void()>) {}
     ^
c++11-lambda-type.cc:4:6: note: candidate function
void foo(std::function<bool()>) {}
     ^
1 error generated.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment