c++ - Extern template with variadic arguments doesn't compile -
i try create extern template variadic arguments like:
extern template<typename... xs> void log( xs... xs ); but gcc 7.2 doesn't compile it, , show error:
error: expected unqualified-id before ‘<’ token i check gcc status in c++11, , extern templates should work, isn't it?
the extern keyword different expect - if understand correctly expect, of course.
the extern keyword applied explicit instantiations of template, , prevents compiler generating implicitly code template while processing translation unit. per paragraph 14.7.2/2 of c++11 standard:
there 2 forms of explicit instantiation: explicit instantiation definition , explicit instantiation declaration. an explicit instantiation declaration begins
externkeyword.
without extern keyword, compiler generate code (say) log(double, int) in each translation unit contains calls log(double, int), , code - , should identical translation units - merged linker (the linker discard duplicates , keep one).
the extern keyword saves waste of compilation time telling compiler: "trust me, else instantiate template somewhere else - don't need now". but promise must fulfilled.
so instance, if have primary template:
template<typename... xs> void log(xs... xs); and declare explicit instantiation:
extern template void log(int, double); than must have corresponding explicit instantiation in translation unit:
template void log(int, double) otherwise, compiler never ever produce code log<int, double>(int, double), , linker complain undefined references.
Comments
Post a Comment