“elaborated type refers to typedef” error when trying to befriend a typedef

后端 未结 3 1516
醉梦人生
醉梦人生 2021-01-12 09:43

Let\'s say I have the following piece of code (a simple CRTP class hierarchy). I want to typedef the base class type to save myself typing (in my actual code, I use the base

相关标签:
3条回答
  • 2021-01-12 09:45

    Enable C++11 only and use friend BaseType

    You cannot use friend class on typedef in C++03.

    An elaborated-type-specifier shall be used in a friend declaration for a class(101)

    101) The class-key of the elaborated-type-specifier is required.

    elaborated-type-specifier:
    
    class-key ::opt nested-name-specifieropt identifier
    
    class-key ::opt nested-name-specifieropt templateopt template-id
    
    enum ::opt nested-name-specifieropt identifier
    
    typename ::opt nested-name-specifier identifier
    
    typename ::opt nested-name-specifier templateopt template-id
    
    0 讨论(0)
  • 2021-01-12 09:48

    I believe that this is not possible with C++03, but was added to C++11 in which you can simply omit the class keyword:

    friend BaseType;
    
    0 讨论(0)
  • 2021-01-12 10:04

    You actually can do it in C++ earlier than C++11, but it requires a fairly elaborate (haha) workaround, along the lines of, 1st declare this "helper":

    template< class T > struct ParamTypeStruct { typedef T ParamType; };
    

    Then, your friend declaration:

    friend class ParamTypeStruct< BaseType >::ParamType;
    
    0 讨论(0)
提交回复
热议问题