need info compiler generated member function

Hi,

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 ?



Thanks
This can be checked by writing a small program that uses these implicitly declared operations
and verifying that it compiles and runs without errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#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)
}

http://coliru.stacked-crooked.com/a/65aa4ca843f3164d
Thanks for the reply. is there any way , i can check code being generated by compiler by reading obj or binary ? i
Then you would need to learn to reverse engineer assembly code. Better to reverse engineer the C++ code itself, no?
> 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
Topic archived. No new replies allowed.