Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out which functions the compiler generated?

Tags:

c++

c++11

I know about compiler-generated functions, the rule of three and the rule of five. In real-world scenarios, it may not be trivial to figure out exactly which of the compiler-generated functions (constructors, assignment operators, destructor) were actually created by the compiler.

Is there any way to list the compiler-generated functions for a specific class?

I am primarily interested in Visual Studio 2019 and Xcode, but a generic solution would be even more welcome.

like image 511
Helge Klein Avatar asked Jan 06 '20 17:01

Helge Klein


People also ask

What default functions for a class does the compiler provide?

The special member functions are class (or struct) member functions that, in certain cases, the compiler automatically generates for you. These functions are the default constructor, the destructor, the copy constructor and copy assignment operator, and the move constructor and move assignment operator.

What functions are generated by compiler automatically in C++?

In C++, the compiler automatically generates the default constructor, copy constructor, copy-assignment operator, and destructor for a type if it does not declare its own. These functions are known as the special member functions, and they are what make simple user-defined types in C++ behave like structures do in C.

Why compiler not provide some functions so that we have to write those functions?

When the compiler generates them. In some cases, the compiler won't generate those functions. If you write any of those functions yourself, the compiler won't generate it. That's pretty obvious.


2 Answers

The rules are complicated. I will steal from another answer which quotes a table from Howard Hinnant's presentation.

enter image description here

The moral here is that a good practice is to not rely on compiler implicit declares and explicitly declare every special member (as defaulted or deleted, depending on your needs)

like image 130
bolov Avatar answered Nov 10 '22 14:11

bolov


"Is there any way to list the compiler-generated functions for a specific class?"

Of course there is. On Linux (and other Unix systems) you can use nm, readelf and objdump on the generated object files/libraries/executable to disassemble them and inspect any exported symbols (and much more).

There are similar tools on Windows, I know, but that's not a platform I work much with, so unfortunately I cannot name exact tool names there.

like image 7
Jesper Juhl Avatar answered Nov 10 '22 14:11

Jesper Juhl