Using CRTP http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern有时我会写这样的代码:
// this was written first
struct Foo : Base<Foo, ...>
{
...
};
// this was copy-pasted from Foo some days later
struct Bar : Base<Foo, ...>
{
...
};
很难理解出了什么问题,直到我在调试器中跟踪代码并发现 Bar 的成员没有在Base
.
如何在编译时显示此错误?
(我使用MSVC2010,所以我可以使用一些C++0x功能和MSVC语言扩展)
在 C++0x 中,您有一个简单的解决方案。我不知道它是否在MSVC10中实现。
template <typename T>
struct base
{
private:
~base() {}
friend T;
};
// Doesn't compile (base class destructor is private)
struct foo : base<bar> { ... };
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)