Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

According to §7.3.1.2/3 the call to f() below is not a friend of class A::X::Y. How come it can access the private static member int i = 1000?

$
0
0

clang compiles this code, printing 1000. I don't know how that is possible!

VS2013 doesn't compile it, as I was expecting.

#include <iostream>
template <class T> void f(T);  

namespace A {
    struct X {
        class Y {
            friend void f<>(int);
            static const int i = 1000;
        };
    };
}

template <class T> void f(T t) { std::cout << A::X::Y::i << '\n'; }


int main()
{
    f(10);
}
 

If I transfer the template declaration and the definition into the namespace A, as shown below, then VS2013 works correctly printing 1000, because now the function A::f() is a friend of the class A::Y::X.

#include <iostream>

namespace A {
    template <class T> void f(T);
    struct X {
        class Y {
            friend void f<>(int);
            static const int i = 1000;
        };
    };
    template <class T> void f(T t) { std::cout << A::X::Y::i << '\n'; }
}




int main()
{
    A::f(10);
}



Viewing all articles
Browse latest Browse all 15302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>