public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/68089] New: c++ friend with template
@ 2015-10-25 16:20 dushistov at mail dot ru
2015-10-31 6:37 ` [Bug c++/68089] " redi at gcc dot gnu.org
0 siblings, 1 reply; 2+ messages in thread
From: dushistov at mail dot ru @ 2015-10-25 16:20 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68089
Bug ID: 68089
Summary: c++ friend with template
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: dushistov at mail dot ru
Target Milestone: ---
Here is code:
#include <iostream>
template<typename T>
class C
{
template<typename U> // ????????
friend class C;
template<typename U>
friend bool operator == (C const& c1, C<U> const& c2)
{
return c1.i == c2.i;
}
int i;
public:
C(): i {0} {}
C(int val): i {val} {}
}; // template class C
int main()
{
C<int> c1 {5};
C<char> c2 {5};
if (c1 == c2) std::cout << "All ok" << std::endl;
else std::cout << "Very bad" << std::endl;
return 0;
}
compile line:
g++ -Wall -std=c++14 test4.cpp
g++ -Wall -std=c++14 test4.cpp
test4.cpp: In instantiation of 'bool operator==(const C<T>&, const C<U>&) [with
U = char; T = int]':
test4.cpp:28:13: required from here
test4.cpp:15:9: error: 'int C<char>::i' is private
int i;
^
test4.cpp:12:19: error: within this context
return c1.i == c2.i;
so even if bool operator == (C const& c1, C<U> const& c2) is friend of
template<typename T> class C; it not allowed to access C<>::i,
but clang compiles this code without errors.
^ permalink raw reply [flat|nested] 2+ messages in thread
* [Bug c++/68089] c++ friend with template
2015-10-25 16:20 [Bug c++/68089] New: c++ friend with template dushistov at mail dot ru
@ 2015-10-31 6:37 ` redi at gcc dot gnu.org
0 siblings, 0 replies; 2+ messages in thread
From: redi at gcc dot gnu.org @ 2015-10-31 6:37 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68089
--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The error seems correct to me, the function is only a friend of its first
argument, not its second. Friendship is not transitive, just because the
function friends with C<T> and C<T> is friends with C<U> that does not mean the
function is friends with C<U>.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2015-10-31 6:37 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-25 16:20 [Bug c++/68089] New: c++ friend with template dushistov at mail dot ru
2015-10-31 6:37 ` [Bug c++/68089] " redi at gcc dot gnu.org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).