Timeline for Storing C++ template function definitions in a .CPP file
Current License: CC BY-SA 2.5
10 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Mar 14, 2024 at 6:10 | comment | added | Davis Herring | @KonradRudolph: Function templates are not implicitly inline, although they do have similar behavior with regard to the ODR and the linker. (This distinction matters in other situations, including the explicit instantiations mentioned here.) | |
| Feb 18, 2021 at 18:43 | comment | added | ph3rin |
@DanNissenbaum I think the non-exported refers to the export keyword.
|
|
| May 6, 2015 at 23:39 | comment | added | osirisgothra | watch out, recursive Q&A :) | |
| Jun 12, 2014 at 21:41 | comment | added | Konrad Rudolph |
@Dan Function templates are implicitly inline. The reason being that without a standardised C++ ABI it’s hard/impossible to define the effect that this would otherwise have.
|
|
| Jun 12, 2014 at 21:06 | comment | added | Dan Nissenbaum | For exported functions (a typical case, if I understand correctly), though, from the quoted paragraph, would it not then be true that the definition of the template function does not need to be present in a translation unit in which the template function is explicitly instantiated? If so, this seems to violate the implication of the accepted answer, the answer being that it is a correct approach to explicitly instantiate the template function in the same source file (and hence the same translation unit) as where it is defined. If you have time, I'd love to have that clarified! Thanks. | |
| Jun 12, 2014 at 20:02 | comment | added | Konrad Rudolph |
@Dan Trivial counterexample: inline functions
|
|
| Jun 12, 2014 at 19:46 | comment | added | Dan Nissenbaum |
Thanks. I thought that all functions are (by default) visible outside the compilation unit. If I have two compilation units a.cpp (defining the function a() {}) and b.cpp (defining the function b() { a() }), then this will successfully link. If I'm right, then the above quote would seem not to apply for the typical case... am I going wrong somewhere?
|
|
| Jun 12, 2014 at 19:32 | comment | added | Konrad Rudolph | @Dan Visible only inside its compilation unit, not outside it. If you link multiple compilation units together, exported symbols can be used across them (and must have a single, or at least, in the case of templates, consistent definitions, otherwise you run into UB). | |
| Jun 12, 2014 at 19:29 | comment | added | Dan Nissenbaum | What does non-exported mean? | |
| Sep 22, 2008 at 16:15 | history | answered | Konrad Rudolph | CC BY-SA 2.5 |