自动确定用户定义的函数是否等效于隐式函数 [英] Automatically determine if user-defined function is equivalent to the implicit one

查看:42
本文介绍了自动确定用户定义的函数是否等效于隐式函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,用户使用与其隐式定义的版本等效的功能来实现功能。例如,一个复制构造函数仅调用其所有成员的复制构造函数。

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) { }
}

这是不希望的,因为这会导致额外的维护,例如,如果类成员被重命名/重新排序等,并且会降低可读性。另外,添加这些函数还意味着诸如 std :: is_trivially_copy_constructable 之类的函数不能简单地构造复制类型(但实际上实际上是可以的)。

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).

推荐答案

我的建议是不要尝试以编程方式确定这些功能是否与默认实现相同,因为差异实际上可能是一个错误(而且它们应该具有正常的默认行为)。

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.

这篇关于自动确定用户定义的函数是否等效于隐式函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆