multiple definition of template specialization when using different objects

后端 未结 4 645
清酒与你
清酒与你 2020-11-30 18:23

When I use a specialized template in different object files, I get a \"multiple definition\" error when linking. The only solution I found involves using the \"inline\" func

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 19:04

    The keyword inline is more about telling the compiler that the symbol will be present in more than one object file without violating the One Definition Rule than about actual inlining, which the compiler can decide to do or not to do.

    The problem you are seeing is that without the inline, the function will be compiled in all translation units that include the header, violating the ODR. Adding inline there is the right way to go. Otherwise, you can forward declare the specialization and provide it in a single translation unit, as you would do with any other function.

提交回复
热议问题