Gcc error? Problem with controlling access to friend function in template class

I have a template class and I am defining a friend function inside the class.

#include <iostream>
using namespace std;

template <typename T>
class template_class {
    T v;
    friend void foo(template_class t) {
        t.v = 1;    // (1)can access the private member because it a friend
        cout << t.v << endl;
        template_class<int> t1;
        t1.v = 2;   // (2)accessible if instantiated with [T=int]
        cout << t1.v << endl;
        template_class<char> t2;
        t2.v = 'c'; // (3)should not be accessible too if instantiated with [T=int]
        cout << t2.v << endl;
    }
};

int main() {
    template_class<int> t;  // (4)generate void foo(template_class<int> t)
    foo(t);
    return 0;
}

      

If my understanding is correct, (4) generate a function void foo(template_class<int>)

and make it a friend template_class<int>

so it can access a private member template_class<int>

like (1) and (2) above source. But (3) should not be OK either, it is not a friend template_class<char>

, it void foo(template_class<char>)

will only be a friend template_class<char>

.

EDIT As @Constructor and @Chnossos said, the above source is compiled with gcc 4.8.1 , but not clang 3.4 . So which one is correct? Is this just a gcc bug? Does the standard have a clear definition of this case?

+2


source to share


1 answer


As dyp says in the comments, this is just a GCC bug. Either PR 41437 or one of the others that PR 59002 references to.



+1


source







All Articles