Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Template friend of templated class

14 views
Skip to first unread message

Juha Nieminen

unread,
May 28, 2018, 5:29:21 AM5/28/18
to
This compiles ok:

//------------------------------------------------------------
struct Test
{
template<typename T>
friend Test friendFunc(T);

private:
int value;
};

template<typename T>
Test friendFunc(T value)
{
Test test;
test.value = value;
return test;
}

int main() { friendFunc(10); }
//------------------------------------------------------------

However, this does not:

//------------------------------------------------------------
template<typename R>
struct Test
{
template<typename T>
friend Test<R> friendFunc(T);

private:
int value;
};

template<typename T>
Test<T> friendFunc(T value)
{
Test<T> test;
test.value = value;
return test;
}

int main() { friendFunc(10); }
//------------------------------------------------------------

It complains that the member variable is private.

How to make the second example compile as well?

Chris Vine

unread,
May 28, 2018, 6:07:34 AM5/28/18
to
If you change the friendship declaration of friendFunc in Test to:

template<typename T> friend Test<T> friendFunc(T);

so that it matches the definition, it will compile. The templatizing
of Test on R is pointless and presumably you really want the 'value'
member to be of type R?

Chris
0 new messages