Possible Duplicate:
How to declare a friend class conditionally?
This question branches off from Can friend class be declared conditionally in C++03?. Specifically, does C++11 provide any additional options to help with conditionally declaring a friend class? That is, is it at all possible to do this in C++11?
Going through cplusplus.com, I came across std::enable_if
. I tried using it, but could not figure out the right syntax. Is std::enable_if
the right construct to use for this purpose? Below is the code I tried, based on the example given there. I do not really need a template here, but I do not know how to avoid it, since all the example codes given there use it.
class Foo {
template<typename T> struct std::enable_if<true, T> {
typedef T Bar;
friend class Bar;
};
};
This gives the following compile error message:
prog.cpp:5:36: error: 'enable_if' is not a template
prog.cpp:5:55: error: qualified name does not name a class before '{' token
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.