This question already has answers here:
Defining a member function inside the class instead of outside in C++?
(4 answers)
Closed 7 years ago.
Is it true that if a function body is defined inside a class, the compiler will mark it inline? (even if not marked by the writer)
example:
class F {
public:
void func() {
std::cout << "is this inline?\n";
}
};
Yes.
[C++14: 9.3/2]: A member function may be defined (8.4) in its class definition, in which case it is an inline member function (7.1.2), or it may be defined outside of its class definition if it has already been declared but not defined in its class definition. [..]
However, whether this has any observable effects beyond the associated linkage requirements is only as predictable as the inline keyword ever is.
The reason for this rule is so that it is legal to include the class definition — member functions and all — via a header into multiple translation units. You would have multiple reference linker errors otherwise.
If you state the body of a function inside a class, it is equivalent to defining that function outside the class and prefixing it with the inline keyword, but whether or not it will be inlined is ultimately up to the compiler.
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:
C++ static member variable and its initialization
(5 answers)
How to initialize private static members in C++?
(18 answers)
Closed 5 years ago.
I know that non-constant static variables need to be initialized outside the class definition but, is there a reason for this?
class A {
static int x = 0 // compile error;
static int y;
};
int A::y = 0; // fine
Essentially it's because x exists independently of the number of instances of A that are created.
So storage for x needs to be defined somewhere - you can't rely on an instance of A to do that, and that's what
A::x = 0;
in exactly one translation unit, does.
When the const qualifier is present, the static variable can be considered as a constant expression. Initializing it in the class definition goes to that effect. It's just some constant value, may not even need any storage.
But in the other case, it's not a constant expression. It definitely needs storage. And as #Bathsheba points out, it needs to be defined in only one translation unit (pre-C++17). Generally speaking, a declaration that contains an initializer is also a definition. So it just can't be initialized when declared.
Starting with C++17, that variable can be an inline variable. So the definition can in fact be included with the class declaration
class A {
static inline int x = 0;
};
And the compiler will sort out all those declarations to mean the same storage.
after a small research, found this (from bogotobogo) :
we cannot initialize a static member variable inside the class declaration. That's because the declaration is a description of how memory is to be allocated, but it doesn't allocate memory. We allocate and initialize memory by creating an object using that format.
In the case of a static class member, we initialize the static member independently, with a separate statement outside the class declaration. That's because the static class member is stored separately rather than as part of an object.
The exception to the initialization of a static data member inside the class declaration is if the static data member is a const of integral or enumeration type.
my take from this is ..
static members exist as members of the class rather than as an instance in each object of the class.
when you initialize the static variable inside the class declaration, as a concept it will be re-initialized (not the actual behaviour) on every creation of an object/instance of the class,
[since the class declaration is the blueprint of which every new object of the class is construct].
but we know that this is not supposed to be the behavior of a static member, so the initialization of this member is outside of the class declaration.
I found this explanation a more intuitive one, but still the formal explanation remains the first one.
Apart from what others have said, there is currently no place (pre C++ 11) within a class where you could initialize a static member (because members (both static and non-static can't be initialized where declared). For non-static members, we use either the constructor or the member initializer list to do the initialization. But this means we have to create an instance of the class.
Since a static member initialization can't depend on an instance being created, it's done outside the class where the member is declared.
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 should I write the keyword 'inline' for a function/method?
(16 answers)
Closed 9 years ago.
I am trying to completely understand what inline does but I get confused when it comes to constructors. So far I understood that inlining a function will put the function in the place it is called:
class X {
public:
X() {}
inline void Y() { std::cout << "hello" << std::endl; }
};
int main() {
X x;
x.Y; //so instead of this it will be: "std::cout << "hello" << std::endl;"
}
But what will this do (how does this get replaced?):
class X {
public:
//or inline if the compilers feeling friendly for this example
__forceinline X() {}
}
The meaning of inline is just to inform the compiler that the finction in question will be defined in this translation as well as possibly other translation units. The compiler uses this information for two purposes:
It won't define the function in the translation unit as an externally visible, unique symbol, i.e., you won't get an error about the symbol being defined multiple times.
It may change its view on whether it wants to call a function to access the finctionality or just expand the code where it is used. Whether it actually will inline the code will depend on what the compiler can inline, what it thinks may be reasonable to inline, and possibly on the phase of the moon.
There is no difference with respect to what sort of function is being inlined. That is, whether it is a constructor, a member function, a normal function, etc.
BTW, member functions defined inside the class definition are implicitly declared inline. There is no need to mention it explicitly. __forecedinline seems to be a compiler extension and assuming it does what it says is probably a bad idea as compilers are a lot better at deciding on what should be inlined than humans.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Do static members of a class occupy memory if no object of that class is created?
Memory Allocation of Static Members in a Class
"A class is not considered defined untill its class body is complete, a class can not have data members of its own type. A class can have data members that are pointers/reference to its own type."
C++ Primer (Lippman Lajoie)
Makes sense.
But why is this allowed then ?
class justAClass
{
public :
justAClass();
private :
static justAClass justAMember;
}
For pointers it is understandable. But how will this above thing work ? How will i ever decide the size for object of such a class ? Isnt it a recursive case (with no base condition) to have a member of its own type, even if it is static ?
The reason for class can't have data members of its own type is the compiler must know the size of class object.
For example, one class is a local variable in function, the compiler can handle the stack only it knows the class size.
For your case, the static class member doesn't reside in class object, so has no impact to size of class object. It's OK.
Formally, the distinction is that the declaration of a static member in a class is not a definition. You must provide a definition elsewhere (exactly once), and the compiler doesn't need to know the size until it encounters the definition. Static members do not impact on the size of the class itself. (In many ways, the static member declaration in the class is very much like an extern non-member declaration.)