【问题标题】:Why can templates only be implemented in the header file?Why can templates only be implemented in the header file?
【发布时间】:2022-12-02 13:12:04
【问题描述】:

Quote from The C++ standard library: a tutorial and handbook:

The only portable way of using templates at the moment is to implement them in header files by using inline functions.

Why is this?

(Clarification: header files are not the only portable solution. But they are the most convenient portable solution.)

  • While it is true that placing all template function definitions into the header file is probably the most convenient way to use them, it is still not clear what\'s \"inline\" doing in that quote. There\'s no need to use inline functions for that. \"Inline\" has absolutely nothing to do with this.
  • Book is out of date.
  • A template is not like a function which can be compiled into byte code. It is just a pattern to generate such a function. If you put a template on its own into a *.cpp file, there is nothing to compile. Moreover, the explicite instanciation is actually not a template, but the starting point to make a function out of the template which ends up in the *.obj file.
  • Am I the only one who feels that the template concept is crippled in C++ due to this?...
  • @AnT perhaps they meant \"inline\" not as the keyword but rather as \"methods implemented at the place of declaration, inside the class\".

标签: c++ templates undefined-reference c++-faq


【解决方案1】:
【解决方案2】:
【解决方案3】:
【解决方案4】:
【解决方案5】:
【解决方案6】:
【解决方案7】:
【解决方案8】:
【解决方案9】:
【解决方案10】:
【解决方案11】:
【解决方案12】:
【解决方案13】:
【解决方案14】:
【解决方案15】:
【解决方案16】:
【解决方案17】:
猜你喜欢
  • 2018-07-28
  • 2022-12-01
  • 2022-12-01
  • 1970-01-01
  • 2022-12-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-05
相关资源
最近更新 更多