G++ generates code for unused template specializations?

前端 未结 2 979
太阳男子
太阳男子 2021-02-04 15:17

In a bit of serialization code for a project I\'m working on I have a type whose size is compiler dependent. In order to deal with this, I decided to use a template specializati

2条回答
  •  礼貌的吻别
    2021-02-04 15:46

    The template specializations in your example are functions with external linkage. The compiler cannot know that they won't be called from another translation unit.

    On my g++ 4.7.2 Ubuntu system, placing the templates into an anonymous namespace and compiling with -O3 prevented the unused function from being generated.

    Similarly, declaring the function template static had the desired effect.

提交回复
热议问题