What is practical difference between `inline` and `template<class = void>`?

  • A+
Category:Languages

We have 2 methods to declare function in header-only library. They are inline and template<class = void>. In boost source code I can see both variants. Example follows:

inline void my_header_only_function(void) {    // Do something...    return; }  template<class = void> void my_header_only_function(void) {    // Do something...    return; } 

I know what is difference according to C++ standard. However, any C++ compiler is much more than just standard, and also standard is unclear often.

In situation where template argument is never used and where it is not related to recursive variadic template, is there (and what is) practical difference between 2 variants for mainstream compilers?

 


I think this can be used as a weird way to allow library extension (or mocking) from outside library code by providing specialization for void or a non-template version of the function in the same namespace:

#include <iostream>  template<class = void> int foo(int data) {     ::std::cout << "template" << std::endl;     return data; } // somewhere else int foo(int data) {     ::std::cout << "non-template" << std::endl;     return data; }  int main() {     foo(1); // non template overload is selected     return 0; } 

online compiler

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: