-1

I know the one definition rule and why the function definition should not be put in the header file if you want to use it in multiple source files.

However, I don't quite get the reason why we can put the function template in the header file. During the compile time, the compiler creates an instantiation. Now aren't there still two function definitions for "int sum(int, int)" for each translation unit as the example below.

// main.cpp
include "helper.hpp"

int main() {
    sum(2, 3);
    return 0;
}

// helper.hpp
template <typename T>
T sum(T a, T b) {
    return a + b;
}

// helper.cpp
#include "helper.hpp"

int sum2(int a, int b)
{
    return sum(a, b);
}
MinLinC
  • 47
  • 2
  • The one definition rule is sadly not as simple as you posed it in your opening paragrpah (which is only true for non inline functions). It is rather more involved and also deals with inline functions/variables, types, and templates. Suffice it to say, the rule also deals with "identical" definitions in different TUs,. – StoryTeller - Unslander Monica Sep 02 '23 at 19:13
  • Note: the answer linked as a duplicate isn't about function templates specifically, but the same exemption from the ODR applies in general. – Jan Schultke Sep 02 '23 at 19:14

1 Answers1

-2

Just because the standard says so.

Template would not be practically usable otherwise, so they can work that way (and for example if in the template body implementation you use a static variable, there must be only one of those static for each instantiation (e.g. one for int and a separate one for float).

C++ is a mixed bag of weird rules: e.g. you cannot call a function without first seeing its prototype... except in the class where you can call methods even if they're defined later in the class. Why does that work? Just because the standard says so.

Don't try to look too hard into logic reasons, there are none. Just learn the rules... and don't try too much on guessing, intuition is not a good guide in C++.

6502
  • 112,025
  • 15
  • 165
  • 265
  • "Just because the standard says so" and "Just learn the rules" and "C++ is a mixed bag of weird rules" doesn't really answer the question. It just vaguely hints at a possible answer. – Jan Schultke Sep 02 '23 at 19:19
  • @JanSchultke: I think the only correct answer is what I wrote in the first line. You can try to rationalize it, or to explain because of the compilation model, but that doesn't make it logical. This happens VERY often in C++: for example local classes could not be used to instantiate templates a few years back, no reason... and now they can (thanks god); but they cannot have inline friend methods... (?) still no reason and may be it's a limitation that will be lifted in the future, may be not. We'll just have to wait for next standard. Logic is not a strong player in C++. – 6502 Sep 02 '23 at 19:35
  • 1
    If "the standard says so" is a reasonable answer, then you could provide a citation of the relevant wording, and you could provide rationale for why this wording exists. This would be a good answer. – Jan Schultke Sep 02 '23 at 19:37
  • @JanSchultke: then look it up and write it. To me seems the OP perfectly understood the rules and is asking WHY. And (as often happens with C++) there's no real why... at most there are excuses; this is what my answer is about. If you look too much at logic your C++ life won't be happy and productive. Does it make sense that `std::string s; s=3.14159265359;` compiles with no errors and no warnings? No... but it's this way and you better know. – 6502 Sep 02 '23 at 19:46