CppCon 2018: Walter E. Brown “C++ Function Templates: How Do They Really Work?”

CppCon · 25K views · 2018 · 1:00:18

Open on YouTube

C, C++, Rust, Go & systems languagestalk

Why Stack Overflow recommends it

Stack Overflow users link it when explaining why function template specializations behave oddly in overload resolution and why overloads are usually the better choice.

Recommended by 5 people across 8 different months, 2018 to 2023.

What it is

A CppCon 2018 talk by Walter E. Brown on how C++ function templates work: synthesized declarations, overload resolution and partial ordering, instantiation, and why explicit specialization is best avoided in favor of overloading. It closes with advice on customizing standard library function templates and customization points.

What people said

Strong rec Peter Ruderman · 2018-11 · Stack Overflow · on “How to implicitly convert class template object when passing as argument to function template?”

The problem is that template argument deduction does not consider implicit conversions, so when you write myFunc(foo) the compiler is unable to determine a type for T. And that's really the end of the story. See Template argument deduction [LINK] on C++ reference. Walter E. Brown gave a fantastic talk about how function templates work (including why you should call them function templates and not templatized functions ) at CppCon2018 which I highly recommend. See " C++ Function Templates: How Do They Really Work? [this video] " on youtube.

Strong rec NathanOliver · 2019-09 · Stack Overflow · on “Template specialization vs. Function overloading”

It makes no difference in this case, but there are cases where it will. Lets look at this sample code template<class T> // (a) void f(T); template<> // (b) void f<>(int*); template<class T> // (c) void f(T*); int main() { int *p; f(p); } When we call f(p); , what function would you expect to be called? Most people would go with b , and they would be wrong. The reason for this is because specializations do not apply to overload resolution. They are a special recipe for a template that tells the compiler that if you deduce this type, then stamp out the template this way instead. So what the compiler does is go through overload resolution and says I can call a with T being int* , or I can call

Recommends Bob__ · 2020-02 · Stack Overflow · on “"specializations don’t participate in overloading"”

If you can spare some time, you may find useful to watch this talk by Walter E. Brown about function templates [this video] .

Recommends IInspectable · 2020-05 · Stack Overflow · on “See reference to function template instantiation message when catch std::exception in Visual Studio 2019”

The issue is rooted in your use of the term *"template function"*. What you really have is a *"function template"* that needs to be instantiated to produce code. This misunderstanding prevents you from understanding your code and the error message. To fix that watch Walter E. Brown's talk on function templates [this video] .

Recommends Casey · 2021-05 · Stack Overflow · on “What are the priorities when calling template and non template functions?”

As Walter E Brown teaches in his 2018 CppCon talk [this video] there are levels of priority for templates based on specialization: non-template overloads are always picked first; non-templates are more specialized than templates themselves. specialized templates (your template <> void f(float) ); the way I remember it is these types of templates "specify" their template arguments. generic templates (i.e. less specialized) are picked last.

Recommends NathanOliver · 2021-06 · Stack Overflow · on “Cleanly handling void as generic return type of labmda in C++”

FWIW, instead of specializing, you should overload. For one, it's less typing and easier to read. Secondly function template specializations are second class citizens in C++ and have confusing rules. Here is a great video on the subject and details some of the reasons why function template specializations for standard library functions has been removed: [this video]

Recommends NathanOliver · 2022-07 · Stack Overflow · on “C++ template specialization problem with const char*”

FWIW this can be easily solved by turning those specializations into overloads. Here is a great video that gets into some of the pitfalls of using specialization: [this video]

Recommends Casey · 2022-09 · Stack Overflow · on “Template specialization for variadic template”

Obligatory Function Templates: How Do They Work? [this video] reference. (Pertinent info at 41:12, but highly recommend watching entire thing)

Cites dfrib · 2020-06 · Stack Overflow · on “What is the difference between std::enable_if and std::enable_if_t?”

C++14 (N3655): Alias Templates for Traits C++14 integrated all but part 4 of N3655: TransformationTraits Redux, v2 [LINK] written by well-renowned template metaprogramming guru Walter E. Brown ✝ . What is the difference between it [ std::enable_if_t ] and std::enable_if ? Are there any advantages or differences in using std::enable_if_t ? The _t alias templates for accessing the underlying type of a metafunction, typename metafunction-name<metafunction-argument(s)>::type , has not been added solely as syntactic sugar to language , but also to lessen the burden on inexperienced (Brown: "[...] inexpert" ) metaprogramming developers when facing compiler error messages whilst working with metafu

Cites Evg · 2021-07 · Stack Overflow · on “Error std::bad_array_new_length in a template”

@LukeTheWolf "templated class" and "templated function" are widely used, but, as Walter Brown says [this video] , words order does matter: "milk chocolate" and "chocolate milk" are very different. Class templates and function templates.