How do I force a particular instance of a C++ template to instantiate?

后端 未结 6 601
误落风尘
误落风尘 2020-11-29 23:07

See title. I have a template. I want to force a particular instance of a template to instantiate. How do I do this?

More specifically, can you force an abstract temp

6条回答
  •  忘掉有多难
    2020-11-29 23:23

    You can force instantiation by using the template with the desired parameter. For example you could define a function using all the required methods:

    void force_int_instance() {
      Abstract *a;
      a->some_method();
      a->some_other_method(1, 2, 3);
    }
    

    You don't need to actually call that function anywhere, so it's not a problem that the pointer is not initialized. But the compiler has to assume that the function might be called from another object file, so it has to instantiate the template.

提交回复
热议问题