Browse DevX
Sign up for e-mail newsletters from DevX


Change the Rules of C++ with Defaulted Functions-3 : Page 3




Building the Right Environment to Support AI, Machine Learning and Deep Learning

A Trivial Question
Defaulted functions solve two problems: they are more efficient than manual implementations and they free the programmer from the burden of defining those functions manually.

C++ makes a distinction between trivial and nontrivial special member functions. This distinction isn't purely academic; a class that has one or more nontrivial special member functions isn't a POD type. This raises another question: are defaulted functions trivial?

An inline defaulted function can be trivial. To qualify as trivial, a defaulted function must be identical to the function that the compiler would implicitly declare without the =default specifier. Additionally, the defaulted function must be inline. Consider:

struct S { inline S()=default; };

The defaulted constructor of S is trivial because the compiler would have implicitly-declared a trivial constructor for S by default:

struct S { //no user-declared canonical functions. Hence, S //has implicitly-declared trivial constructor, dtor etc. };

However, the following defaulted destructor isn't trivial:

struct S { inline virtual ~S()=default; };

The defaulted destructor is inline, which is the first criterion for qualifying as trivial. However, this destructor is virtual, whereas the implicitly-declared destructor would have been non-virtual. Therefore, the defaulted destructor of S is not trivial.

Danny Kalev is a certified system analyst and software engineer specializing in C++. He was a member of the C++ standards committee between 1997 and 2000 and has since been involved informally in the C++0x standardization process. He is the author of "The ANSI/ISO Professional C++ Programmer's Handbook" and "The Informit C++ Reference Guide: Techniques, Insight, and Practical Advice on C++."
Thanks for your registration, follow us on our social networks to keep up-to-date