Template constructor does not work in MSVC due to collision of member function names with argument type

The following code snippet fails to compile in MSVC 18.00.31101 with below error, but succeeds in gcc 4.9.2 and clang 3.6.0. Determining the type of the argument or including the keyword struct in the declaration resolves the error. Is this a compiler error or undefined behavior?

#include <cstdlib>

struct A {
    int B;
};

struct Snap {
    template<size_t TSize>
    Snap(const A (&)[TSize]) {
        // do something with TSize
    }

    void A() {}
};

int main() {
    A pop[] = { {1}, {2}, {3} };

    Snap crackle(pop);

    return 0;
}

      

...

1> <...>: error C2664: 'Snap::Snap(const Snap &)' : cannot convert argument 1 from 'A [3]' to 'const Snap &'
1>          Reason: cannot convert from 'A [3]' to 'const Snap'
1>          No constructor could take the source type, or constructor overload resolution was ambiguous

      

+3


source to share


1 answer


It is poorly formed but does not require diagnosis. [Basic.scope.class] / p1:

2) The name N

used in the class S

must refer to the same declaration in its context and when reevaluating in the completed scope S

. no diagnostics are required to violate this rule.



A name A

evaluated in its context refers to ::A

, but refers to Snap::A

when overridden in a terminated scope Snap

.

+4


source







All Articles