I read that for any empty class, compiler generates five member functions.
1. constructor, 2. Destructor, 3 copy c'tor, 4 assignment operator 5. reference operator.
how to check that ? i couldnt able to validate in obj or binary file.
is there any way we can check these functions ?
#include <cassert>
int main()
{
struct empty_class {};
// verify that an implicitly declared public default constructor is present
// (this would have generated a compile-time error if empty_class is not default constructible)
empty_class default_constructed_object ;
// verify that an implicitly declared public copy constructor is present
// (this would have generated a compile-time error if empty_class is not copy constructible)
empty_class a_copy{ default_constructed_object } ;
// verify that an implicitly declared public copy assignment operator is present
// (this would generate a compile-time error if empty_class is not copy assignable)
a_copy = default_constructed_object ;
// verify that the address of an object of type empty_class can be taken
// (this would have generated a compile-time error if this is not the case)
empty_class* pointer = &default_constructed_object ;
// verify that a pointer to empty_class can be dereferenced
// (this would have generated a compile-time error if this is not the case)
empty_class& reference = *pointer ;
assert( pointer == &reference ) ; // verify that these have normal semantics
// we have also verified that an implicitly declared public destructor is present
// (note: the life-times of the two objects end when we return from main)
// (a compile-time error would have been generated if empty_class is not destructible)
}
> any way , i can check code being generated by compiler by reading obj or binary ?
For an empty class, no code would be generated.
The implicitly declared default constructor and destructor would be trivial (no code).
The implicitly declared copy constructor and copy assignment operator would also be trivial,
and there would be nothing to copy or assign (no code again). https://gcc.godbolt.org/z/O8esyu
We can see the (typically inline) code generated when there is something to be done for these operations.
For example: https://gcc.godbolt.org/z/6rP42P