ZWI #2 – zaprzyjaźniona pojedyńcza specjalizacja szablonu

Do napisania tej notki zainspirowało mnie to pytanie na forum. Lekko parafrazując podany kod, szablon klasy wyglądał następująco:

template<typename T>
void bar(Foo<T> f)
template<typename T>
struct Foo
    Foo(T val) : val{val} {}
    template<typename U>
    friend void bar(Foo<U>);
    T val;

Powyższy kod niepotrzebnie1 zaprzyjaźnia wszystkie specjalizacje bar. Na przykład, dla Foo<int>, zaprzyjaźniona zostanie zarówno bar<int>, jak i każda inna specjalizacja, chociażby bar<float>. W efekcie legalna będzie również taka specjalizacja:

void bar<int>(Foo<int> x)
    Foo<float> y{static_cast<float>(x.val)};

W zdecydowanej większości przypadków nie ma to sensu, a zamierzeniem jest zaprzyjaźnienie wyłącznie specjalizacji po tym samym typie, po którym konkretyzowana jest klasa. W takim wypadku można zaprzyjaźnić konkretną specjalizację:

template<typename T>
struct Foo
    // ...
    friend void bar<T>(Foo<T>);
    // ...

Lub, krócej, wykorzystując dedukcję typów i injected class name:

template<typename T>
struct Foo
    // ...
    friend void bar<>(Foo);
    // ...

1Zakładając, że autor chciał zaprzyjaźnić wyłącznie bar<T>, ale nie wiedział jak. Tak było w przypadku wspomnianego wątku.

20 thoughts on “ZWI #2 – zaprzyjaźniona pojedyńcza specjalizacja szablonu

  1. 161746 93067Youre so cool! I dont suppose Ive learn something like this before. So nice to search out any person with some special thoughts on this topic. realy thank you for starting this up. this internet site is one thing thats required on the net, someone with a bit of originality. valuable job for bringing something new to the internet! 872632

  2. It’s the best time to make some plans for the future and it’s time to be happy.

    I have read this post and if I could I wish to suggest you few
    interesting things or advice. Maybe you could write next articles referring to this article.
    I wish to read even more things about it!

  3. Heya i am for the primary time here. I came across this board
    and I in finding It truly useful & it helped me out a lot.
    I’m hoping to present something again and aid others such as you helped me.

  4. Hello there! This article could not be written much better!
    Looking through this article reminds me of my previous roommate!
    He constantly kept talking about this. I’ll send this information to him.
    Pretty sure he’s going to have a great read. I appreciate you for sharing!

  5. An impressive share! I have just forwarded this onto a co-worker who has
    been conducting a little research on this. And he in fact ordered me breakfast
    due to the fact that I stumbled upon it for him… lol.
    So allow me to reword this…. Thanks for the meal!! But
    yeah, thanks for spending time to talk about
    this topic here on your web page.

  6. Excellent post. I used to be checking constantly this blog and I am
    inspired! Extremely helpful info particularly the ultimate phase
    🙂 I maintain such info a lot. I used to be looking for this
    certain information for a very long time. Thanks and best of luck.

  7. I used to be suggested this web site via my cousin. I’m now not
    positive whether this submit is written by him as nobody else recognise such designated approximately my trouble.
    You’re incredible! Thanks!

  8. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get three emails
    with the same comment. Is there any way you can remove people from that service?
    Bless you!

  9. Great weblog right here! Also your site lots up very fast!
    What host are you the use of? Can I am getting
    your associate hyperlink to your host? I desire my site loaded up as fast as yours lol

  10. Hi! I’ve been following your site for a long time now and finally got the courage to go
    ahead and give you a shout out from Atascocita Tx!
    Just wanted to say keep up the excellent work!

  11. I’ll immediately grab your rss as I can’t to find your email subscription link
    or e-newsletter service. Do you’ve any? Kindly allow me understand in order that I
    could subscribe. Thanks.

  12. Hi i am kavin, its my first occasion to commenting anyplace, when i read this article i
    thought i could also make comment due to this sensible paragraph.

  13. Thanks for some other excellent post. Where else may anybody
    get that kind of information in such a perfect approach
    of writing? I have a presentation subsequent week, and I’m on the search for such information.

  14. I’m pretty pleased to find this website. I want to to thank you
    for ones time due to this wonderful read!! I definitely loved every
    part of it and i also have you book-marked to check out
    new stuff in your web site.

  15. Hi there! I simply want to give you a huge thumbs up for
    the great info you’ve got here on this post. I am returning to your site for more soon.

Leave a Reply

Your email address will not be published.