Skip to content

Instantly share code, notes, and snippets.

@zxytim
Last active August 29, 2015 14:05
Show Gist options
  • Save zxytim/7cb140d035e9d8c41f11 to your computer and use it in GitHub Desktop.
Save zxytim/7cb140d035e9d8c41f11 to your computer and use it in GitHub Desktop.
strange behavior of auto type deduction in c++14
/*
$ g++ a.cc -o a -std=c++1y
$ ./a
3.1
3.1
3
3.1
3
3.1
3.1
*/
#include <iostream>
auto add(auto a, auto b) {
return a + b;
}
auto add2(auto a, auto b) {
return b + a;
}
template<class A, class B>
auto add3(A a, B b) {
return a + b;
}
int main() {
std::cout << 1 + 2.1 << std::endl;
std::cout << add(1.1, 2) << std::endl;
std::cout << add(1, 2.1) << std::endl;
std::cout << add2(1.1, 2) << std::endl;
std::cout << add2(1, 2.1) << std::endl;
std::cout << add3(1.1, 2) << std::endl;
std::cout << add3(1, 2.1) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment