Using typedefs as friends

This is a discussion on Using typedefs as friends within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm having a problem using a typedef'd name in a friend class declaration. What I'm trying is variations ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    88

    Using typedefs as friends

    Hi all,

    I'm having a problem using a typedef'd name in a friend class declaration. What I'm trying is variations on the following:

    Code:
    struct Type
    {
      typedef OtherType other_type;
    };
    
    class A
    {
    public:
      typedef typename Type::other_type other_type;
      // ...
    private:
      friend typename other_type;
    };
    Surely there's a way to make A:ther_type a friend class, or am I mistaken? I can't help feel I'm missing something obvious here. Any pointers would be much appreciated.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    You shouldn't have the "typename" keyword anywhere. This is not a template.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Quote Originally Posted by brewbuck View Post
    You shouldn't have the "typename" keyword anywhere. This is not a template.
    Oops, of course. That wasn't the problem though. I just got it working, but I can't say I understand why one works (Works) and not the other (Broken).

    Code:
    template<typename Type>
    struct Traits
    {
      typedef OtherType other_type;
    };
    
    template<typename SomeType>
    class Broken
    {
      typedef typename Traits<SomeType>  traits;
      typedef typename traits::other_type other_type;
      friend class other_type;
    };
    
    template<typename SomeType>
    class Works
    {
      typedef typename Traits<SomeType> traits;
      friend class traits::other_type;
    };
    At least, 'Broken' doesn't work on GCC 4.1, but I guess it's a language not a compiler issue. Oh well, thanks anyway.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Ok, I've experimented a bit, but I'm still hazy on the correct way to do this. I'd appreciate some advice, as my searches have been mostly fruitless.
    Code:
    template<typename T>
    struct traits
    {
      typedef T type;
    };
     
    template<typename V>
    struct some_struct
    {
      typedef traits<V>       traits_type;
      typedef traits<V>::type friend_type;
     
      friend class traits_type::type;    // works with GCC, not with MSVC
      friend typename traits_type::type; // works with MSVC, not with GCC
      friend typename friend_type;       // doesn't work
      friend class friend_type;          // doesn't work
    };
    Can anyone explain which, if any, compiler has it right? (note: MSVC means 7.1 and 8.0) Is this the wrong way to go about this?

    P.S. I did find this:
    According to Section 7.1.5.3 paragraph 2 of Standard C++:

    3.4.4 describes how name lookup proceeds for the identifier in an elaborated-type-specifier. ... If the identifier resolves to a typedef-name or a template type-parameter, the elaborated-type-specifier is ill-formed. [Note: this implies that, within a class template with a template type-parameter T, the declaration

    friend class T;

    is ill-formed. ]
    IIUC, this means the "don't work" lines above are expected to break, but I'm not so sure about the other two...

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    A friend declaration MUST name a class. You cannot access this class type via a typedef. MSVC is wrong in this case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows Typedef's
    By valaris in forum Windows Programming
    Replies: 2
    Last Post: 04-25-2009, 09:32 AM
  2. Friends and Members
    By MarlonDean in forum C++ Programming
    Replies: 6
    Last Post: 06-18-2008, 11:52 AM
  3. typedefs in C language
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 03-28-2007, 10:58 PM
  4. problem with friends
    By noob2c in forum C++ Programming
    Replies: 8
    Last Post: 09-15-2003, 07:43 AM
  5. Help me, Friends and Gurus ????
    By yescha in forum C Programming
    Replies: 3
    Last Post: 12-03-2001, 02:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21