C ++ functions without destructor

I am writing a simple C ++ application that will go into an infinite loop and then never exit.

I have a series of objects that will be built at the top of main () and never leave the scope. Memory (for both code and RAM) is of great value as I only have a few k bytes to work with.

Will the optimizer remove unused destructors altogether for me? If not, is there a way to tell the compiler not to generate default destructors?

Also, there is a similar way to get rid of some of the other default functionality that the classes are associated with (copy constructor, etc.).

+3


source to share


2 answers


With C ++ 11, the default destructor and some other member functions can be removed. For a class, A

this can be done with.

 ~A() = delete;

      

However, removing the destructor does introduce some restrictions on how the instance is created.



For older (pre C ++ 11) compilers, just don't declare a destructor. While the compiler usually creates a destructor, it will often be something that is built in and does nothing, so the compiler may choose to exclude the code entirely.

You will also need to read your compiler documentation (or study the code it emits) to understand what it actually does. When it comes to eliminating unused code, including delete

d member functions in C ++ 11, you rely on the quality of your compiler's implementation. You may also find that different optimization settings affect what it does (for example, does the empty constructor generated by the compiler actually not exist).

+3


source


If you are using C ++ 11, the default destructor can be removed. Check the code below:



class A
{
    public:
        ~A() = delete;
};

      

+1


source







All Articles