From dba6bebd616fc92602c738fbdf3a898691660f92 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Tue, 26 Apr 2022 11:47:51 +0200 Subject: static-vs: move TL;DR to top --- ...06-24-static-vs-inline-vs-unnamed-namespaces.md | 34 +++++++++++----------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/_posts/2017-06-24-static-vs-inline-vs-unnamed-namespaces.md b/_posts/2017-06-24-static-vs-inline-vs-unnamed-namespaces.md index f58c50a..fca1d7a 100644 --- a/_posts/2017-06-24-static-vs-inline-vs-unnamed-namespaces.md +++ b/_posts/2017-06-24-static-vs-inline-vs-unnamed-namespaces.md @@ -45,6 +45,23 @@ snippets: In this post I'll try to figure out whether I should use `static`, `inline` or unnamed namespaces for function definitions. +TL;DR +----- + +Here's my attempt to build an algorithm to decide whether a class/function +should be defined with either of the `static`/`inline` specifiers or put into +an unnamed namespace. +The first question I answer is: is the entity defined in a header file or in a +.cpp file? + +* **In a header** — Is it a class or a function? + * **Class** — There's no need to do anything. + * **Function** — Do you want it to behave differently for each +translation unit (may be useful, for example, for logging)? + * **Yes** — Use `static`. + * **No** — Use `inline`. +* **In a .cpp file** — Put it into an unnamed namespace. + `static` -------- @@ -210,23 +227,6 @@ of a function/class, including their own local static variables, etc. * Defining different classes with the same name in different translation units (without utilizing unnamed namespaces) is undefined behavior. -Conclusion ----------- - -Here's my attempt to build an algorithm to decide whether a class/function -should be defined with either of the `static`/`inline` specifiers or put into -an unnamed namespace. -The first question I answer is: is the entity defined in a header file or in a -.cpp file? - -* **In a header** — Is it a class or a function? - * **Class** — There's no need to do anything. - * **Function** — Do you want it to behave differently for each -translation unit (may be useful, for example, for logging)? - * **Yes** — Use `static`. - * **No** — Use `inline`. -* **In a .cpp file** — Put it into an unnamed namespace. - Tricky cases ------------ -- cgit v1.2.3