multiple definition of template specialization when using different objects

后端 未结 4 635
清酒与你
清酒与你 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条回答
  •  -上瘾入骨i
    2020-11-30 19:09

    Intuitively, when you fully specialize something, it doesn't depend on a template parameter any more -- so unless you make the specialization inline, you need to put it in a .cpp file instead of a .h or you end up violating the one definition rule as David says. Note that when you partially specialize templates, the partial specializations do still depend on one or more template parameters, so they still go in a .h file.

提交回复
热议问题