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

 ̄綄美尐妖づ 提交于 2019-12-31 01:47:07

问题


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) {
        friend class Bar;
    }
};

I did not find any solution on the internet. I went through all the answers to the question Generating Structures dynamically at compile time. Many of them use the C++11 std::conditional, but I would like to know if it is possible to do this in C++03 without using the preprocessor.

This solution https://stackoverflow.com/a/11376710/252576 will not work because friendship is not inherited ( friend class with inheritance ).

Edit Just to make this more easily visible, as mentioned below in the comment: This requirement is unusual. This is part of a new research project in hardware simulation, that I am working on. The testbench is written in C++, and I want to display the variables in a waveform. I have researched various other options, and figured out that I need to use a friend class, due to practical considerations. The friend will capture the values and generate the waveform, but I would prefer to have the friend only when the waveform is required, and not all the time.


回答1:


Use friend std::conditional<C, friendclass, void>::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<C, 
    friendclass, detail::friendclass>::type::friendclass;
};

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




回答2:


[class.friend]/3 tells this :

A friend declaration that does not declare a function shall have one of the following forms:
friend elaborated-type-specifier ;
friend simple-type-specifier ;
friend typename-specifier ;

therefore it is not possible to conditionally declare friends of a class, without a macro.




回答3:


It seems, unfortunately, not possible within the C++ compiler: ie, it seems that only the preprocessor may help here. Note: Johannes has a proposal, so there is hope!

However I would note that:

  • friendship does not require you to actually use it
  • friendship is a pure compile-time construct (like access specifiers) and does not incur any runtime penalty on any major compiler

there is no reason not to have unconditional friendship, but only use it if some conditions (static or dynamic) are met.

Note: in the future, this is something that the static_if proposal could cover.




回答4:


Note: Johannes has pretty much nailed it. In '03 you cannot befriend a typedef - but if you know you have a class, then you can refer to it's injected class name.

Johannes' answer also has the benefit of using standard library functionality which too is always a good thing.

#define some_compile_time_condition 0

class Foo;

template <int Condition> class  TestCondition {
private:
  friend class Foo;
  struct Type {
    struct Bar;
  };
};

template <> class TestCondition<1> {
public:
  typedef Bar Type;
};

struct Bar
{
public:
  void foo (Foo &);
};

class Foo {
private:
  friend struct TestCondition< some_compile_time_condition >::Type::Bar;
  int m_i;
};

void Bar::foo (Foo & foo)
{
  foo.m_i = 0;
}

It's still different to the requirement in that Foo always has a friend, but the befriended class changes based on the value of the option.

An interesting side question is whether it is an ODR violation to have versions of Foo both with and without some_compile_time_condition set to 1.




回答5:


I think you take 1 preprocessor and write your source code inside that.

bool flag = false;
#ifdef _MY_FRIEND_
    friend class sample
    flag = true;
#endif

if (flag)
{
 ...
 ...
 ...
}

class Foo {
#ifdef _MY_FRIEND_
    friend class Bar;
#endif
}

};

Here _MY_FRIEND_ is a preprocessor and if you add that preprocessor then at compile time your class Bar will be the friend class...you can use that preprocssor any where when you want to need class Bar as a friend class.other wise compile without the preprocessor then it wont allow u to add Bar as a friend class of Foo

Please correct me if i understood the question wrong.



来源:https://stackoverflow.com/questions/13985292/can-friend-class-be-declared-conditionally-in-c03

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!