nholthaus / units

a compile-time, header-only, dimensional analysis and unit conversion library built on c++14 with no dependencies.
http://nholthaus.github.io/units/
MIT License
959 stars 137 forks source link

Incomplete constexpr support #113

Open JohelEGP opened 6 years ago

JohelEGP commented 6 years ago

Greetings.

I tried using some units in a constexpr context and found that constexpr is partially supported. Here's an extract of what I tried to run, with the operations that lack constexpr support commented out.

constexpr void test_constexpr()
{
    using namespace units::literals;

    units::length::meter_t m{42};
    //    +m;
    //    -m;
    //    ++m;
    //    --m;
    //    m++;
    //    m--;
    m + m;
    m - m;
    m * 42;
    42 * m;
    m / 42;
    m / m;
    //    m += 42_m;
    //    m -= 42_m;
    //    m *= 42;
    //    m /= 42;
}

I tested this on the branch v3.x. The library uses C++17 in some places, so I suggest improving the constexpr support, which should be doable even with C++14. I did check some of the operation's implementations, and sprinkling constexpr should do the job. I didn't check the support for the dimensional-analysis operators nor the cmath's-like operations.

nholthaus commented 6 years ago

looks like an oversight when I added those operators. The dimensional analysis should all be constexpr, but if you find holes obviously let me know.

The cmath functions, aside from specialized pow/sqrt, are not constexpr, because right now they are ADL wrappers on top of the actual cmath functions, which are not constexpr as of c++17. I've been looking for a good constexpr cmath library to pull in, but haven't settled on anything yet.