c++ - Overload resolution produces ambiguous call when template parameters added -
in following code compiler can resolve call f()
call f(int&, const char*)
.
the call g()
, however, ambiguous. lists 4 overloads possible overload set. if remove , typename t2, std::size_t i
template argument list array argument, , hard code them instead, there no ambiguity , compiler picks g(t&, const char*)
.
how adding 2 template arguments makes ambiguous? can see how, though decay , casting, resolve 1 of overloads, cannot figure out how adding template parameters introduces ambiguity.
i have testing on clang 3.8 (unreleased) , vc++ 2015.
#include <string> void f(int&, const char*){} void f(int&, char(&)[8]){} void f(int&, bool){} void f(int&, const std::string&){} template <typename t> void g(t&, bool){} template <typename t> void g(t&, const char*){} template <typename t> void g(t&, const std::string&){} template <typename t, typename t2, std::size_t i> void g(t&, t2(&)[i]){} int main(int argc, char* argv[]) { int = 1; f(i, " "); g(i, " "); return 0; }
overload #2:
void g(t&, const char*){}
overload #4:
template <typename t, typename t2, std::size_t i> void g(t&, t2(&)[i]){}
overload #2 , overload #4 ambiguous when calling g(...)
string literal; string literal argument deduced const char*
t2(&)[i]
.
reason
your string literal " "
const char[8]
, shows how gets deduced overload #4. however, since arrays decay pointers, const char[8]
const char*
deduces overload #2.
Comments
Post a Comment