Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've finally understood what the problem was... and your solution does not solve much, if any.</p> <p>The goal of using local static variable is to provide initialization on first use, thus being safe from the "Initialization Order Fiasco" (by the way, it does not solve the "Destruction Order Fiasco").</p> <p>But with your design, if you effectively prevent the <code>crash</code> you do not however prevent the issue of using a variable before its value is used.</p> <pre><code>ImportantObject&lt;tbt&gt; SinceSliceBread; // using an empty string tbt::set_name("xyz"); </code></pre> <p>Compare with the following use:</p> <pre><code>std::string&amp; tbt::get_name() { static std::string MName = "xyz"; return MName; } </code></pre> <p>Here the <code>name</code> is not only created but also <strong>initialized</strong> on first use. What's the point of using a non initialized name ?</p> <p>Well, now that we know your solution does not work, let's think a bit. In fact we would like to automate this:</p> <pre><code>struct tag { static const std::string&amp; get_name(); static const std::string&amp; get_fs_location(); }; </code></pre> <p>(with possibly some accessors to modify them)</p> <p>My first (and easy) solution would be to use a macro (bouh not typesafe):</p> <pre><code>#define DEFINE_NEW_TAG(Tag_, Name_, FsLocation_) \ struct Tag_ \ { \ static const std::string&amp; get_name() { \ static const std::string name = #Name_; \ return name; \ } \ static const std::string&amp; get_fs_location() { \ static const std::string fs_location = #FsLocation_; \ return fs_location; \ } \ }; </code></pre> <p>The other solution, in your case, could be to use <code>boost::optional</code> to detect that the value has not been initialized yet, and postpone initialization of the values that depend on it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload