lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi] Don't initialize constexpr variable with std::ldexp()


From: Vadim Zeitlin
Subject: [lmi] Don't initialize constexpr variable with std::ldexp()
Date: Mon, 24 Apr 2017 14:24:08 +0200

 Hello,

 Both clang and MSVC refuse to compile the current lmi sources because of a
constexpr variable being initialized with a non-constexpr std::ldexp() call.

 Clang says:

        In file included from calendar_date.cpp:28:
        bourn_cast.hpp:173:27: error: constexpr variable 'limit' must be 
initialized by a constant expression
            static constexpr From limit = std::ldexp(From(1), 
to_traits::digits);
                                  ^       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        bourn_cast.hpp:323:12: note: in instantiation of function template 
specialization 'bourn_cast<int, double>' requested here
            return bourn_cast<To,From>
                   ^
        calendar_date.cpp:694:32: note: in instantiation of function template 
specialization 'bourn_cast<int, double>' requested here
                calendar_date z((jdn_t(bourn_cast<int>(candidate))));
                                       ^
        bourn_cast.hpp:173:35: note: non-constexpr function 'ldexp' cannot be 
used in a constant expression
            static constexpr From limit = std::ldexp(From(1), 
to_traits::digits);
                                          ^
        /usr/include/x86_64-linux-gnu/bits/mathcalls.h:106:13: note: declared 
here
        __MATHCALL (ldexp,, (_Mdouble_ __x, int __exponent));
                    ^

 And MSVS gives shorter but equally clear:

        bourn_cast.hpp(173): error C2131: expression did not evaluate to a 
constant (compiling source file ledger_text_formats.cpp)
                 bourn_cast.hpp(173): note: failure was caused by call of 
undefined function or one not declared 'constexpr' (compiling source file 
ledger_text_formats.cpp)
                 bourn_cast.hpp(173): note: see usage of 'ldexp' (compiling 
source file ledger_text_formats.cpp)

 They both seem quite correct with respect to the current language
standard, while gcc seems to implement the extensions proposed in
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0533r0.pdf
and which might become available in a later language version, but we can't
rely on them yet.

 So the attached patch simply replaces "constexpr" with in "limit"
declaration with "const" to fix compilation with clang and MSVC (while
still continuing to compile with gcc, of course).

 Notice that I didn't change the "static" keyword even though I'm not
really sure if it's appropriate to use it here and, moreover, if I'm quite
sure that it's not useful to use it with "constexpr" in the
is_twos_complement declaration just below. Maybe this should be addressed
too...

 As always, thanks in advance for applying this patch,
VZ

Attachment: 0001-Do-not-make-variable-initialized-with-ldexp-constexp.patch
Description: Text document


reply via email to

[Prev in Thread] Current Thread [Next in Thread]