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

前端 未结 3 837
抹茶落季
抹茶落季 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 12:48

    I would separate the implementation and the interface, using template specialization:

    lib1.h:

    #include 
    template  void print_me(void);
    template <> void print_me<0>(void);
    template <> void print_me<1>(void);
    

    lib1_internal.h (NOTE: this does not need to be disclosed):

    #include 
    
    template
    void print_me_internal() {
        std::cout << "I am function number " << N << std::endl;
    }
    

    lib1.cpp:

    #include "lib1.h"
    #include "lib1_internal.h"
    
    template <> void print_me<0>() {
      print_me_internal<0>();
    }
    template <> void print_me<1>() {
      print_me_internal<1>();
    }
    

    your main.cpp will correctly lead to a linker error:

    $ g++ main.cpp -L. -l1
    /tmp/ccZSDqkp.o: In function `main':
    main.cpp:(.text+0xf): undefined reference to `void print_me<2>()'
    collect2: ld returned 1 exit status
    

    Just add the definition of template void print_me(void) in lib1.h in place of its declaration and it will be used whenever not found in the specialized versions.

提交回复
热议问题