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 friend
ship 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.
[class.friend]/3 tells this :
therefore it is not possible to conditionally declare friends of a class, without a macro.
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.
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 withoutsome_compile_time_condition
set to 1.I think you take 1 preprocessor and write your source code inside that.
};
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.
Use
friend std::conditional<C, friendclass, void>::type;
whereC
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
Note that the detail dummy class name needs to match the name of the potential friend in this workaround.
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:
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.