-
-
Save zxytim/7cb140d035e9d8c41f11 to your computer and use it in GitHub Desktop.
strange behavior of auto type deduction in c++14
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
$ 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