c++ templates and ambiguous call to overloaded function

I'm stuck with the following problem. I was using Xcode 7 and had no issue with my project. After trying to compile it on Visual Studio Express 2015, I get the message

"Error C2668 ambiguous call to overloaded function"

in the code. I couldn't find anything specific to visual studio related to the problem.

I made a minimal working example supposed to be used on VS (there's no error on Xcode). The weird part involves the

func2

part. It's as if VS compiler couldn't auto deduce more types and/or arguments than a limit.

Update :

A workaround was proposed by Sam Varshavchik consisting in using partial specialization with an intermediate template class. This is a solution that I would like to avoid. First because it's not convenient in the context it would apply in my code, second because this compilation error is unclear to me. This error doesn't show up in Xcode7, and func2 has no error even in VS. Despite the explanation of WhiZTiM to which I agree with, fact is, overloading in this context can sometimes work, and sometimes not. And I'd really like to know why.

Update 2 :

According to bogdan, this is probably a bug in GCC and MSVC. I'm going to try to report it. (I love so much this first week with visual studio)

This looks like a bug in MSVC and GCC. The call should resolve to the second overload (Clang and EDG are doing that).

For the call B::func(g, dt, bx), name lookup finds the two func templates. Template argument deduction and substitution is performed on each of them in order to generate function template specialization declarations that can subsequently participate in overload resolution. Deduction succeeds for both templates and we're left with two specializations:

The two functions have identical parameter declaration clauses, so clearly overload resolution cannot distinguish between them based on conversions from the arguments of the call; it has to resort to the last two steps in the process.

First, if one of the functions is a template specialization and the other one is not, the non-template one is preferred; not applicable here.

Last, it looks at the templates from which the two specialization declarations were synthesized; if one of the templates is more specialized than the other according to the partial ordering of function templates, then the corresponding specialization is preferred. (This is the only place in the process where the original templates come back into play.)

The description below is not very accurate and skips quite a few details, but I'm trying to concentrate on the parts that are relevant to this case. Very roughly:

First, references and cv-qualifiers are stripped from the function parameter declarations of both templates, yielding:

Then, deduction is attempted as if for a call to one template using the forms of the function parameters of the other template as arguments, and then the other way around. In this case, the last two pairs of corresponding parameters have identical forms, so deduction succeeds both ways. For the first pair of corresponding parameters:

Deducing M from an argument of the form G<T2<U>, V> works; M is deduced as G<T2<U>, V>.

Deducing T2, U and V in G<T2<U>, V> from an argument of the form M doesn't work (M can be anything).

In other words, G<T2<U>, V> is a "more specific" form than M; it cannot represent all the types that M can represent; this is the intuitive meaning that more specialized is trying to formalize in this context.

So, deduction works for all pairs of corresponding parameters from F2 to F1, but not the other way around. This makes F2 more specialized than F1 in terms of partial ordering.

This means that the specialization corresponding to the second template is preferred in overload resolution.