This question already has answers here:
Closed 10 years ago.
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.
[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.
Related
This question already has answers here:
When should I write the keyword 'inline' for a function/method?
(16 answers)
Closed 1 year ago.
I've been reading a c++ book, C++ Primer, and i was going through the class features and everything, and i encountered that, in a class most functions ( or every) are inline automatically.
What difference does it really make? explicitly defining an inline function vs implicitly defining an inline function, we could have already overloaded them inside the class scope anyways, i am finding it very difficult to understand this part. Is there any kind of performance gain by doing explicitly ?. in the photo we can see get() function using both methods explicit & implicit, can someone clarify me here.
I have to edit the question, because I've been told so many times that class members functions are not automatically inline. But the book c++ primer and other internet sources say that they are automatically inline.
Here is a piece of text from the book..
If a definition of a member function is within the class body it is implicitly inline. If you only declare it in the class body and you place the definition outside of it you need to make it explicitly inline.
This can be done in two ways:
struct test {
inline void foo();
};
void test::foo() {
}
Or
struct test {
void foo();
};
inline void test::foo() {
}
While both work, the second option is generally recommended.
This question already has answers here:
Is there any difference if we define friend function inside or outside of class
(6 answers)
Closed 5 years ago.
In C++, we are allowed to define a friend function inside the class definition
like:-
class A {
public:
A(int a): mem(a){}
~A() {}
friend void fun() {}
private:
int mem;
};
void fun();
and then we can call this function, just like any regular function.
fun();
Can someone explain about (with examples):
In what cases do we need to define friend function inside the class
definition.
What is special about this kind of definition which can not be
achieved with just declaring function as friend in class and then
defining the function outside.
Assuming that you already know what is a friend function, there is absolutely no special meaning to your example: what you have is a regular friend function, with its declaration and definition combined.
Recall that friendship needs to be declared inside the class that "friends" a function. After that, the function can be defined at some place, for which you have two choices:
Outside the class - that is the common way of defining a friend function, or
Inside the class - that is what your example has.
Basic considerations for going with one approach vs. the other are the same as the rules that you use to decide between defining a member-function inside or outside the class.
This question already has answers here:
When can I use a forward declaration?
(13 answers)
Closed 7 years ago.
I'm having a hard time understanding template class implementation in C++. I understand what a template class is and how to use it, but I cannot seem to implement them properly. This is for school so I cannot use standard library list/etc. I have made a template List class that acts as a linked list using a template node class. I have a third class bigInt which will be used to do infinite precision addition, multiplication, etc. For the bigInt class I get an error when trying to have a variable "values" that is of type List. Why is this? Error: "Error C2079 'bigInt::values' uses undefined class 'List'"
bigInt.h looks like:
template <typename T>
class List;
class bigInt {
public:
List<int> values;
bigInt();
bigInt add(bigInt);
bigInt mul(bigInt);
bigInt pow(int);
};
I added the first two lines because I read somewhere that I needed to use "forward declaration" (since you apparently cannot use an #include "List.h") which I also don't really understand.
Any help would be really appreciated.
You need to completely define the class List<> before you can use it as a member variable. This is usually done by defining the template class in a separate .h file and #includeing it where needed (not sure why you think you can't do this). Alternatively, you can use a pointer to a List without defining it first:
template <typename T>
class List;
class bigInt {
public:
List<int>* values;
/*...*/
}
This question already has answers here:
Can I implement an autonomous `self` member type in C++?
(14 answers)
Closed 7 years ago.
I want to write a macro which, when expanded within a class, uses that class type (specifically, as template argument). Within class method, I can use this:
#define METHOD_MACRO int sample_method(void)const {\
return template_struct<this_type<decltype(this)>::type>::index;}
(this_type is my struct, here it's equivalent to remove_pointer<remove_const<T>>)
But when I need class type outside of method (for typedef for class member pointer), this keyword isn't available; I've tried to use auto for some trick with deducing type, but no luck here.
Classes in question are inherited from my class, if this can be of any help. I would like to avoid anyone using my macro having to write obligatory typdedef.
Any ideas?
You can use the following trick:
#define SELF \
static auto helper() -> std::remove_reference<decltype(*this)>::type; \
typedef decltype(helper()) self
struct A {
SELF;
};
I declare a helper function using the auto return type, which allows me to use decltype(*this) as a return type, not knowing what is the class name. Then I can use decltype(helper()) to use the class type in the code. Note that the function has to be static, otherwise you can not use it in decltype. Also the function is just declared, not defined; this should not be a problem as you are not going to call it anyway. (You can add an empty body to it, but it will raise a warning that a function has no return. Still you may change the return type to be decltype(this) and return nullptr.)
You may then use the self typedef for further declarations, or just alter the macros to typedef not the class itself, but what you need to. Adjust it to suit your particular need.
UPD: This seems to be a non-standard behavior of GCC. For example, ICC does not allow this in static functions even in trailing return type.
This question already has answers here:
what is 'class' in 'class DataType* Variable' in Unreal Engine Shooter Game Sample
(3 answers)
Closed 7 years ago.
I came across a piece of code that looked like this:
class SomeClass* GetSomeClass()
{
return _instanceOfSomeClass;
}
What does the "class" keyword do on the return type? I can't find anywhere that explains what it's function is. Does it just specify that it's talking about SomeClass as a class in case there is some sort of ambiguousness or something? I am confused.
class SomeClass is a longhand way of referring to the class type SomeClass (technically, it's the elaborated type specifier). Usually, adding class is redundant, and the two are equivalent. But it's sometimes necessary to resolve the ambiguity, if there's a variable or function with the same name.
It is used to disambiguate.
Say for example if you have a variable of the same name in the same (or outer) scope, something like this:
int SomeClass; //SomeClass is declared to be variable here
class SomeClass* GetSomeClass()
{
return _instanceOfSomeClass;
}
Without the class keyword, the function declaration wouldn't make sense to the compiler. The class keyword tells the compiler to ignore the variable declaration, and look for a class declaration.
It's a forward declaration. It allows you to just say "there is a class SomeClass somewhere in my program, it is just not visible to this file in order to prevent redeclerations".
Whenever you implement this function, though, the file must have actual interface of class SomeClass.