Note that there are some explanatory texts on larger screens.

plurals
  1. POnon-integral constants
    text
    copied!<p>I want a header file with a non-integral constant in it, e.g. a class. Note the constant does <strong>not</strong> need to be a compile-time constant.</p> <pre><code>static const std::string Ten = "10"; </code></pre> <p>This compiles but is undesirable as each compilation unit now has its own copy of Ten.</p> <pre><code>const std::string Ten = "10"; </code></pre> <p>This will compile but will fail with a linker error for multiply defined Ten.</p> <pre><code>constexpr std::string Ten = "10"s; </code></pre> <p>This would work but only if the strings constructor was constexpr as well. It will be but I can't count on every non-integral constant to have a constexpr constructor ... or can I?</p> <pre><code>extern const std::string Ten = "10"; </code></pre> <p>This seems to work but I'm afraid I'll get a linker error if I breath on it wrong.</p> <pre><code>inline const std::string Ten( ) { return "10"; } </code></pre> <p>This has everything I want except a clean syntax. Plus now I have to refer the constant as a function call, <code>Ten()</code>.</p> <pre><code>inline const std::string = "10"; </code></pre> <p>This seems to be the ideal solution. Of course <code>inline</code> variables aren't allowed by the standard.</p> <ul> <li>Is there something in the c++ standard that says the extern version should work or am I just lucky it works with GCC?</li> <li>Is there a compelling reason not to allow inline variables?</li> <li>Is there a better way with c++03 or will there be a better way in c++0x?</li> </ul>
 

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