harttle/harttle.github.io

2015/07/27/effective-cpp-9

Opened this issue · 2 comments

Effective C++ 9:在析构/构造时不要调用虚函数 | Harttle Land

父类构造期间,对虚函数的调用不会下降至子类。如果这并非你的意图,请不要这样做!

https://harttle.land/2015/07/27/effective-cpp-9.html

实测,MSVC下打开warning as error ,只提示

Warning	C4514	'std::codecvt<unsigned short,char,_Mbstatet>::unshift': unreferenced inline function has been removed	

话说,设置warning as error 不能捕获所有错误吧。那该怎么设置呢?

clang的报错就很友好.

clang++ sc.cpp
sc.cpp:7:3: warning: call to pure virtual member function 'logTransaction' has undefined behavior; overrides of 'logTransaction' in subclasses are not available in the constructor of 'Transaction'
                logTransaction();                         // as final action, log this
                ^
sc.cpp:9:2: note: 'logTransaction' declared here
        virtual void logTransaction() const = 0;      // make type-dependen
        ^
1 warning generated.
sc-7abab0.o : warning LNK4217: locally defined symbol __std_terminate imported in function "int `public: char const * __cdecl type_info::name(void)const '::`1'::dtor$2" (?dtor$2@?0??name@type_info@@QEBAPEBDXZ@4HA)
sc-7abab0.o : warning LNK4217: locally defined symbol _CxxThrowException imported in function "public: void __cdecl std::ios_base::clear(int,bool)" (?clear@ios_base@std@@QEAAXH_N@Z)

注意到这个:

test.obj : error LNK2019: unresolved external symbol "public: virtual void __cdecl Transaction::logTransaction(void)const " (?logTransaction@Transaction@@UEBAXXZ) referenced in function "public: __cdecl Transaction::Transaction(void)" (??0Transaction@@QEAA@XZ)
test.exe : fatal error LNK1120: 1 unresolved externals

看来MSVC还是有错误信息的,不过,不够人性化。