1

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.

Community
  • 1
  • 1
Masked Man
  • 1
  • 7
  • 40
  • 80
  • 2
    Why would you want that? – K-ballo Dec 21 '12 at 06:36
  • What are you trying to achieve? – Nawaz Dec 21 '12 at 06:40
  • @K-ballo I am using this in a new research project in hardware simulation. The testbench code is written in C++, and I want to provide a new feature to see the member variable values in a waveform during the simulation. The friend class will capture the values and generate the waveform. I want that friend to be present only when the waveform is required, and not always. – Masked Man Dec 21 '12 at 06:44
  • 3
    By the way, why the downvote and close? I am asking this question to solve a practical problem. It may be unusual and unheard of for you software guys, but that does not mean I should not be allowed to ask it. – Masked Man Dec 21 '12 at 06:46
  • Please do not close this question. I have explained in the comment above what I am trying to achieve. If this question needs improvement, please explain what you want me to elaborate. – Masked Man Dec 21 '12 at 06:50
  • So you want friendship to depend on template instances at compile time? Class friendship can usually be avoided anyway, maybe if you elaborate a little on your design. – imreal Dec 21 '12 at 06:59
  • @Nick No, I do not really need a template. It is just the syntax I picked from the cplusplus.com page. All the examples there use template, so I thought it was required. I agree with your comment, and I have spent 2 months trying to figure out a way to do it without using friend. Finally, I settled on using friend since I figured it cannot be done. There is 20 years worth of legacy testbench code which the customers of my verification tool are using, and any code rearchitecture at this point is impractical. – Masked Man Dec 21 '12 at 07:04
  • @BЈовић Good observation, but it is not. I am also the author of that question. It looks for a C++03 solution, and in that question, I have also mentioned that I will ask for a C++11 solution separately. I did this to avoid creating two questions in one. – Masked Man Dec 21 '12 at 07:07
  • The question is the same, except in this one you asked whether that can be done using `std::enable_if`. The answer is the same (see below) for both c++11 and c++03. – BЈовић Dec 21 '12 at 07:09
  • @BЈовић Naturally, I do not know that the answer is the same before asking the question. :) In any case, the question is not the same. One looks for a C++03 solution, and the other looks for a C++11 solution. Say, what if it was possible to do it in C++11, but not in C++03? Would you still say the questions are same? – Masked Man Dec 21 '12 at 07:13
  • Yes, they are still the same question. You could have asked there whether there is a different solution for c++11. – BЈовић Dec 21 '12 at 07:20
  • Well okay, I am still trying to get used to the conventions here. Thanks for the clarification. I will take care of that for future questions! :) – Masked Man Dec 21 '12 at 07:27
  • If your classes are not templates (i.e. they are what they are) how can you not know which classes are to be befriended while writing them? Am I missing something? The point of `enable_if` is to check template parameter properties. – imreal Dec 21 '12 at 07:35
  • FWIW, I have "fixed" all the objections raised in the above comments. (I think.) I rewrote to avoid making it seem like I wanted the solution using `std::enable_if` specifically. (The title change also reflects this.) Please consider if you can reopen this question now. If you choose not to do so, that's also fine. – Masked Man Dec 21 '12 at 11:06

1 Answers1

1

[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.

Actually, you can do it with a macro :

class Foo {

#ifdef DECLARE_A_FRIEND
      friend class Bar;
#endif
  };
};

and then define or undefine the macro as a compilation parameter.

BЈовић
  • 62,405
  • 41
  • 173
  • 273