★ 𝐑𝐮𝐢𝐤𝐤𝐚𝐚𝐏𝐫𝐮𝐬 ★

Web & Software developer. I also love pizza🍕, cats🐱, and computers🖥️.

The Astolfo pfp is just temporal!

🎵 𝘖𝘯𝘦 𝘥𝘢𝘺, 𝘐 𝘩𝘰𝘱𝘦 𝘐’𝘮 𝘴𝘰𝘮𝘦𝘰𝘯𝘦 𝘺𝘰𝘶’𝘥 𝘮𝘪𝘴𝘴. 🎶

  • 15 Posts
  • 43 Comments
Joined 1 year ago
cake
Cake day: November 14th, 2023

help-circle



  • Apparently it is impossible for this kind of functions to be defined as friends of classes:

    template <typename T>
    auto do_something(T &t) -> decltype(t.private_msg);
    
    class Foo
    {
    private:
        const char *private_msg = "You can't touch me!";
        
    friend auto do_something<>(Foo &f) -> decltype(f.private_msg); // Error!
    };
    
    template <>
    auto do_something<Foo>(Foo &f) -> decltype(f.private_msg) // Error!
    {
        return f.private_msg; // Error!
    }
    

    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.

    template <typename T>
    auto do_something(T &t) -> decltype(t.private_msg);
    
    class Foo
    {
    private:
        const char *private_msg = "You can't touch me!";
    
    template <typename T>
    friend auto do_something(T &t) -> decltype(t.private_msg); // This works fine!
    };
    
    template <>
    auto do_something<Foo>(Foo &f) -> decltype(f.private_msg)
    {
        return f.private_msg;
    }
    

    Do you think I found an error in the language?