Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically determine if user-defined function is equivalent to the implicit one

Tags:

c++

c++11

Sometimes, users implement functions with the equivalent functionality as their implicitly defined versions. For example, a copy constructor which simply calls the copy constructor of all its members.

struct A
{
    int B;
    A(const A& a) : B(a.B) { }
}

This is undesirable, because it causes additional maintenance, for example if the class members are renamed/reordered, etc., and reduces readability. Also, adding these functions also means that functions such as std::is_trivially_copy_constructable claim the type is cannot be trivially copy constructed (but in practice, it actually could be).

I have a code base where this seems to be a common occurrence, which I would like to rectify, by deleting these implementations. However, I am uneasy about removing functionality that seems to be identical to implicit implementation, in case it might not actually be equivalent. Is there a method for determining whether a function is equivalent to its implicit version? (Using any toolset/language variation/etc is acceptable).

like image 827
MuertoExcobito Avatar asked Oct 30 '22 18:10

MuertoExcobito


1 Answers

My suggestion is to not try to programmatically determine if these functions are the same as the default implementation, because the difference might actually be a mistake (and they were supposed to have the normal default behavior).

Instead I would just suggest to write up a set of unit tests that take care of testing the expected behavior of the various functions, and then make sure they pass on the default implementations. Then not only do you have a test framework for future enhancements you can be confident the functions did what you wanted.

like image 195
Mark B Avatar answered Nov 11 '22 16:11

Mark B