Consider this code:
#include <iostream> void func(int&) { std::cout << "mutable" << std::endl; } void func(const int&) { std::cout << "const" << std::endl; } template<typename T> void tpl_func(T&) { std::cout << "mutable_tpl" << std::endl; } template<typename T> void tpl_func(const T&) { std::cout << "const_tpl" << std::endl; } class number { public: operator int&() { return nb_; } operator const int&() { return nb_; } private: int nb_ = 42; }; int main() { number n; func(n); // This produces: error: call to 'func' is ambiguous tpl_func(n); // This compiles fine } Tested with clang3.5
Questions:
- Why is the overload resolution for the template functions not ambiguous ?
- What rules determine which overload is chosen ?
tpl_func<int>(n);