0

0.cc

template <class T>
T get(){
 return 5;
}

int get(){
 return 6;
}

int main(){
 return get<int>();
}

1.cc

template <class T>
T get(){
 return 7;
}
template int get<int>(); // This forces code generation.

Compiling with g++ -Wall 0.cc 1.cc causes no link errors, returned output is 5.

Questions

1- Do templates have external linkage by default even if extern isn't used?

https://en.cppreference.com/w/cpp/language/storage_duration

names of all templates not listed above (that is, not function templates declared static).

2- Does the linker treat multiple templates like inline functions? i.e it chooses 1 out of many definitions and having different definitions causes UB? https://stackoverflow.com/a/66356946

3- Why doesn't int get(){} cause a link error? do template functions and regular functions have different symbols?

east1000
  • 1,240
  • 1
  • 10
  • 30
Dan
  • 2,694
  • 1
  • 6
  • 19

2 Answers2

3

Does a template function with different definitions cause undefined behaviour?

Yes.

1- Do templates have external linkage by default even if extern isn't used?

Yes, template functions have external linkage unless declared in anonymous namespace or if declared static or if attached to a module and is not exported.

2- Does the linker treat multiple templates like inline functions?

Yes. Implicit instantiations of function templates are treated the same as inline functions in this regard.

and having different definitions causes UB

Yes. Technically, the program is ill-formed, but the distinction between those doesn't really matter at runtime.

3- Why doesn't int get(){} cause a link error?

Overloading function template with a function is well-formed.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • *"Function templates are implicitly inline."* Nah. Multiple definitions of them are allowed, but not because of inline-ness. You can manually mark them `inline` as a hint to the optimizer. – HolyBlackCat Aug 30 '21 at 20:36
  • For question 3, is it legal? This answer says it might be: https://stackoverflow.com/a/16865452 – Dan Aug 30 '21 at 20:36
  • 1
    @HolyBlackCat I adjusted wording. – eerorika Aug 30 '21 at 20:38
  • In the above link `f` is a regular function and also a template. The answer says it's legal to do so. That's also my question #3. – Dan Aug 30 '21 at 20:42
  • 1
    @Dan I see. I thought you meant because of the two different template definitions. I've edited the answer. – eerorika Aug 30 '21 at 20:44
  • ill formed code is free to produce code that does UB even if the ill formed code would never be run. – Yakk - Adam Nevraumont Aug 31 '21 at 01:16
-1

Does a template function with different definitions cause undefined behaviour?

Answerr: Well, Yes, templates do behave undefined if you define them in multiple ways. There should be only one definition

BUT
You can use template in some other scope in with different definition

Do templates have external linkage by default even if external isn't used? Answer:

Yes,
Templates will cause external linkage in normal circum stances but some exceptions are
Defining them in some outer namespace or using them as an exports.module

Does the linker treat multiple templates like inline functions? i.e it chooses 1 out of many definitions and having different definitions Answer: Obviously, Templates are treated as an inline function