Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In my opinion there is not much gain in using literals for Boost.Units, because a more powerful syntax can still be achieved with existing capabilities.</p> <p>In simple cases, looks like literals is the way to go, but soon you see that it is not very powerful. For example, you still have to define literals for combined units, for example, how do you express 1 m/s (one meter per second)?</p> <p>Currently:</p> <pre><code>auto v = 1*si::meter/si::second; // yes, it is long </code></pre> <p>but with literals? </p> <pre><code>// fake code using namespace boost::units::literals; auto v = 1._m_over_s; // or 1._m/si::second; or 1._m/1_s // even worst </code></pre> <p><strong>A better solution can be achieved with existing features. And this is what I do:</strong></p> <pre><code>namespace boost{namespace units{namespace si{ //excuse me! namespace abbreviations{ static const length m = si::meter; static const time s = si::second; // ... } }}} // thank you! using namespace si::abbreviations; auto v = 1.*m/s; </code></pre> <p>In the same way you can do: <code>auto a = 1.*m/pow&lt;2&gt;(s);</code> or extend the abbreviations more if you want (e.g. <code>static const area m2 = pow&lt;2&gt;(si::meter);</code>)</p> <p>What else beyond this do you want?</p> <p>Maybe a <em>combined</em> solution could be the way </p> <pre><code>auto v = 1._m/s; // _m is literal, /s is from si::abbreviation combined with operator/ </code></pre> <p>but there will be so much redundant code and the gain is minimal (replace <code>*</code> by <code>_</code> after the number.)</p> <p>One drawback of my solution is that it polutes the namespace with common one letter names. But I don't see a way around that except to add an underscore (to the beginning or the end of the abbreviation), as in <code>1.*m_/s_</code> but at least I can build real units expressions.</p>
 

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