this post was submitted on 30 Mar 2024
5 points (100.0% liked)
C & C++
59 readers
5 users here now
founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
I think the issue is that
Foo
is incomplete when you're declaring the friend, so I think it's impossible. I just tried it and g++ ignores the target candidate due to "member access into incomplete type", which makes sense sincestd::begin
is already defined and calls.begin()
. The closest you can get is to use another friend to exposearr
and overloadstd::begin
manually, but that's a bit silly 😅Apparently it is impossible for this kind of functions to be defined as friends of classes:
After trying different combinations, it seems that I managed to get it working with the condition the whole template are considered friends of the class. I don't know if I should consider it a language problem, but it seems that way, since the template restrictions (in this case) are minor.
Do you think I found an error in the language?
Ah, nice idea. I've tried a few different ways of doing this, and I think what you're seeing is a discrepancy in how the compiler handles member access into incomplete types. It seems that, in your examples, the compiler is allowing
-> decltype(f.private_msg)
within the class, but I think it's not selectingdo_something
outside of it because it usesdecltype(t.private_msg)
. In my case, I'm not even able to do that within the class.For example, since I'm not able to use
decltype(f.private_msg)
inside the class, I'm usingdecltype(private_msg)
instead, which causes an error at thedo_something
declaration related to incomplete type (presumably because of thet.private_msg
usage):My reasoning is that removing the
t.private_msg
from the declaration works:The reason your second example works is because the friend template inside the class acts as a template declaration rather than a specialization, which isn't specialized until after
Foo
is complete: