Commit 833647bd authored by GILLES Sebastien's avatar GILLES Sebastien
Browse files

Add a new C++ 17 way to define properly a static data attribute.

parent 55df91e8
......@@ -14,7 +14,7 @@
},
"source": [
"<h1>Table of contents<span class=\"tocSkip\"></span></h1>\n",
"<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Static-in-C\" data-toc-modified-id=\"Static-in-C-1\">Static in C</a></span></li><li><span><a href=\"#Static-methods\" data-toc-modified-id=\"Static-methods-2\">Static methods</a></span><ul class=\"toc-item\"><li><span><a href=\"#Nitpick:-ClassName()-is-very-inefficient\" data-toc-modified-id=\"Nitpick:-ClassName()-is-very-inefficient-2.1\">Nitpick: <code>ClassName()</code> is very inefficient</a></span></li></ul></li><li><span><a href=\"#Static-data-attributes---to-avoid...-(see-next-section-to-understand-why!)\" data-toc-modified-id=\"Static-data-attributes---to-avoid...-(see-next-section-to-understand-why!)-3\">Static data attributes - to avoid... (see next section to understand why!)</a></span></li><li><span><a href=\"#Static-order-initialization-fiasco---and-its-fix\" data-toc-modified-id=\"Static-order-initialization-fiasco---and-its-fix-4\">Static order initialization fiasco - and its fix</a></span></li></ul></div>"
"<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Static-in-C\" data-toc-modified-id=\"Static-in-C-1\">Static in C</a></span></li><li><span><a href=\"#Static-methods\" data-toc-modified-id=\"Static-methods-2\">Static methods</a></span><ul class=\"toc-item\"><li><span><a href=\"#Nitpick:-ClassName()-is-very-inefficient\" data-toc-modified-id=\"Nitpick:-ClassName()-is-very-inefficient-2.1\">Nitpick: <code>ClassName()</code> is very inefficient</a></span></li></ul></li><li><span><a href=\"#Static-data-attributes---to-avoid...-(see-next-section-to-understand-why!)\" data-toc-modified-id=\"Static-data-attributes---to-avoid...-(see-next-section-to-understand-why!)-3\">Static data attributes - to avoid... (see next section to understand why!)</a></span></li><li><span><a href=\"#Static-order-initialization-fiasco---and-its-fix\" data-toc-modified-id=\"Static-order-initialization-fiasco---and-its-fix-4\">Static order initialization fiasco - and its fix</a></span><ul class=\"toc-item\"><li><span><a href=\"#Update:\" data-toc-modified-id=\"Update:-4.1\">Update:</a></span></li></ul></li></ul></div>"
]
},
{
......@@ -403,6 +403,38 @@
"* The dedicated item on [Parashift FAQ](https://isocpp.org/wiki/faq/ctors#static-init-order)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Update:\n",
"\n",
"C++ 17 actually provides a way to define the value in the header file with the `inline` keyword:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"struct Class4\n",
"{\n",
" Class4();\n",
" \n",
" ~Class4();\n",
" \n",
" static inline int Ninstance_ = 0;\n",
"};"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Thanks to this [FluentCpp post](https://www.fluentcpp.com/2019/07/23/how-to-define-a-global-constant-in-cpp/) that gave me the hint!"
]
},
{
"cell_type": "markdown",
"metadata": {},
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment