c++ - How to avoid an unreachable code warning due to a compiler bug? -
the cut down reproducible test case, vs2015 update 2, following. attempting build "warning level 4" , treat warnings fatal errors improve chances of noticing said warnings. hoping in community has run , has found reasonable workaround.
this may prove localised if noone else has seen equivalent issue, i'd note underlying question "how badly should 1 mangle codebase evade poor compiler warnings", or equivalently, "how should 1 report bugs compiler vendor loses bug reports".
#ifdef _win32 #pragma warning( push ) #pragma warning( disable : 4702 ) // suggested comments #endif template <class type> void func (type t) { t.func (); t.func (); } #ifdef _win32 #pragma warning( pop ) #endif struct nothrow { void func () {} }; struct throws { void func () {throw 1;} }; int main () { nothrow nt; func (nt); throws t; func (t); }
this triggers unreachable code warnings. templated function looks reasonable itself, 1 particular instantiation compiler able determine second t.func() dead, warns unreachable code.
this seems clear cut quality of implementation issue vs2015, opened bug report here.
we received guidance microsoft,
unfortunately backend of compiler (where warning originates) has no real concept of template function instantiations instances of single template function. they're functions similar looking names, if ever bothered compare (decorated) names eachother. never does. sees here 1 function unreachable code, warns about, , function no unreachable code, not. concept of "cross function warning" propose, gather , compare data across different template instiantiations , warn if or that, extrodinarily difficult under pure ltcg binary , impossible otherwise.
consider template foo in header foo.h. suppose a.cpp includes , creates foo, unreachable code, , b.cpp includes , creates foo no unreachable code. propose in a.cpp foo should not warn, dispite unreachable code, because foo exists no unreachable code. but, foo compiled in different file, in different process, in different invocation of cl.exe, , inconveniently in future. compiler has no ability reach future yet unborn process , pull in information needed calcuate if should warn or not.
the real viable option have here turn off unreachable code warning templates together, i'll honest, isn't going happen until sure harm done greater being done (it's net bad). warnings have false positives, happens. i'll try think of other options, , line number/file thing.
the above link may not available, internet comes caches can see copy here or find own using 2744730 , incorrect-unreachable-code-warning-in-template-instantiation.
so, if assume underlying template instantiation model stamp out copies of functions subject them same warnings analysis other, how might go avoiding dead code warnings? i'm torn between adding tag dispatching dozen or templates affected or switching off warnings globally.
edit: direct link seems live again
i correct behavior, annoying may be. implementer of
template <class type> void func (type t) { t.func (); t.func (); }
could quite happy warning. also, it's not possible compiler sort out if there none, some, type
produce invalid or valid code. therefore, waits until type
specified , continues if code entered fixed type.
Comments
Post a Comment