3

The following example fails to compile in Visual Studio 2015:

namespace ns {
    template<typename T> class MyClass;

    template<typename T>
    T Func(const MyClass<T> &a);

    template<typename T>
    class MyClass {
        // (1) With template type, without namespace: compiles
        //friend T Func<T>(const MyClass<T> &a);
        // (2) With template type, with namespace: compiles
        //friend T ns::Func<T>(const MyClass<T> &a);
        // (3) Without template type, without namespace: compiles
        //friend T Func<T>(const MyClass &a);
        // (4) Without template type, with namespace: doesn't compile
        friend T ns::Func<T>(const MyClass &a);
    };
}

int main() {
    ns::MyClass<int> a;
    return 0;
}

My understanding of injected class names is that MyClass is sufficient to identify MyClass<T> within the confines of the class itself. However depending on the namespace-qualification of Func, Visual Studio 2015 fails to compile the friend definition unless MyClass<t> is used.

Note that this example compiles on at least gcc 5.1. Is this a MSVC bug, or is something else going on here?

jnewman
  • 83
  • 4
  • 1
    I'm pretty sure that the answer is **yes, the name is injected**. Lots of code use this feature when writing custom `friend ostream& operator<<(ostream& lhs, const Foo& foo);`. I don't have a reference so I won't post this as an answer. – vsoftco Jun 09 '17 at 18:14

0 Answers0