Can friend class be declared conditionally in C++03?

前端 未结 5 1397
半阙折子戏
半阙折子戏 2021-01-19 02:54

I want to declare a friend class only if some (compile-time) condition is true. For example:

// pseudo-C++
class Foo {
    if(some_compile_time_condition) {
         


        
5条回答
  •  面向向阳花
    2021-01-19 03:12

    Use friend std::conditional::type; where C is your condition. A nonclass type friend will be ignored.

    The conditional template is easily implemented in C++03. However since C++03 does not support typedef friends you need to use the following syntax there

    namespace detail { class friendclass {}; }
    
    class Foo {
      friend class std::conditional::type::friendclass;
    };
    

    Note that the detail dummy class name needs to match the name of the potential friend in this workaround.

提交回复
热议问题