Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C++ as a standard prohibit the storage of member functions inside individual class instances?

In C++ implementations, typically code is not stored (in any form) inside class instances. The code segment is not in the same memory space as objects and the like. This means that member functions are not "stored" inside class instances.

But when a question was asked about this, I got to wondering: to what extent, if at all, does the standard prohibit member functions being stored inside their encapsulating class, to the extent that instantiating the class makes a copy of those functions? Theoretically, could I make an implementation that worked this way? And could it even remotely abide by the common ABIs?

like image 447
Lightness Races in Orbit Avatar asked Jan 23 '14 18:01

Lightness Races in Orbit


People also ask

What are the different storage classes in a C program?

We have four different storage classes in a C program − The auto storage class is the default storage class for all local variables. The example above defines two variables with in the same storage class. 'auto' can only be used within functions, i.e., local variables.

What is the use of register storage class in C?

You can use the register storage class when you want to store local variables within functions or blocks in CPU registers instead of RAM to have quick access to these variables. For example, “counters” are a good candidate to be stored in the register.

What is static storage class in C++?

Static storage class. The static variables are used within function/ file as local static variables. Static local variable is a local variable that retains and stores its value between function calls or block and remains visible only to the function or block in which it is defined.

What is the storage class of a variable?

Thus a storage class is used to represent the information about a variable. NOTE: A variable is not only associated with a data type, its value but also a storage class. There are total four types of standard storage classes.


1 Answers

If, in C++, code were a first-class value, then the code for a member function would be simply a const static class member, and you would no more expect to find that in an instance than you would any other static data member. (§ 9.4.2: "A static data member is not part of the subobjects of a class.")

However, code is not considered a value, and furthermore you cannot even construct a pointer to a member function (although you can construct a "pointer to member", that is not really a pointer since it is not usable without a reference to an instance). That makes member function code different both from static data members and non-member functions, both of which allow the creation of free-standing pointers, which furthermore have equality guarantees which (more or less) preclude copying.

Class instances do contain a reference to virtual member functions (indirectly, in most implementations; the pointer is actually to a static vtable) which must be copied when a new instance is created. No requirement is made on the size of the reference, so in theory (as far as I know) there is nothing to stop an implementation from avoiding the indirections and storing the entire code anew for each instance of the class.

But there is an exception for standard-layout types, which is a subset of classes with no virtual member functions, expressed in § 9.12/18, which requires that two standard-layout types with identical initial members have identical layout for the initial members. Recalling that standard-layout objects must be simply copyable with memcpy (§3.9/3), must be contiguous in memory (§1.8/5), and must include their members in order (§9.12/13), this requirement makes it effectively impossible to include class-specific static data in any standard-layout object, which would include the code for member functions.

So I conclude that at least for standard-layout objects, the C++ standard does prohibit the storage of static data, including code for member functions, within the object representation.

like image 61
rici Avatar answered Oct 19 '22 03:10

rici