老问题,但仍...
我用几个简单的帮手这一点。它会给出错误链接时那是相当可读:
// Not implemented is not implemented :-)thing, it'll break:
struct NotImplHelper { static void notimplemented(); };
#define notimplemented() NotImplHelper::notimplemented();
#if defined(DEBUG) || defined(_DEBUG)
#define notimplementedvirtual() throw std::exception();
#else
#define notimplementedvirtual() static_assert(false, "You should implement virtual function calls before moving to production.");
#endif
用法:
//lets say this is some class that still doesnt support...
//...all the functionality that it should based on the design docs
void MyClass::MyFunction()
{
notimplemented();
// or notimplementedvirtual() if MyFunction() is virtual...
}
理由:
恕我直言,如果你在程序中使用的功能,它应该是可用。当你尝试编译你还没有实现的东西时,它应该给出编译时或链接时错误。
F.ex.,在MSVC++这样就给:
1>Test.obj : error LNK2019: unresolved external symbol "public: static void __cdecl NotImplHelper::notimplemented(void)" ([email protected]@@SAXXZ) referenced in function "[blahblahblah]"
注意, '引用的函数' 是有在MSVC++。我没有在其他编译器中测试过它。
至于未实现的虚函数调用,你有它的唯一选择抛出异常。在开发过程中没有在调试器中实现这些功能是很好的 - 但是,当问题变得严重时,这些可能会被程序调用,所以它们应该可用。 A static_assert
确保后者。 (所以:结合任何持续集成包,基本上都会失败。)
显然大多数人会不小心混淆了notimplemented
和notimplementedvirtual
。事实上,这不是一个大问题:一个简单的解决方案就是始终使用前者,除非你想摆脱这个错误,因为这是一个WIP。
如果一个函数是_not implemented_(如在,未声明但未定义),那么你将得到一个链接时间错误。在这种情况下,你的功能被实现,并且具有定义良好的行为。 – Chad