Force definition of symbol for a C++ template instance in a library

前端 未结 3 803
抹茶落季
抹茶落季 2021-01-19 12:21

I\'d like to provide a library that provides template code. But I would also like to keep the most possible the ownership of this code (generated code) when I can guess the

3条回答
  •  失恋的感觉
    2021-01-19 13:14

    C++11 solution: use extern templates. Simply add these strings to your main.cpp file:

    extern template void print_me<0>();
    extern template void print_me<1>();
    

    Thus you tell compiler not to instantiate print_me function template in main.cpp (for template arguments 0 and 1). So linker should search definition of void print_me<0>(); and void print_me<1>(); in other translation units.

提交回复
热议问题