Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

constexpr int and constexpr double in c++

I have encountered a strange case.

// this does not work, and reports:
// constexpr variable 'b' must be initialized by a constant expression
int main() {
  const double a = 1.0;
  constexpr double b = a;
  std::cout << b << std::endl;
}

// this works...
int main() {
  const int a = 1;
  constexpr int b = a;
  std::cout << b << std::endl;
}

anything special about double so it cannot make constexpr work?

like image 207
bugatti1001 Avatar asked Oct 23 '19 03:10

bugatti1001


1 Answers

anything special about double so it cannot make constexpr work?

ints and doubles behave differently in this case.

For core constant expression, a in the 2nd case (with type int) is usable in constant expressions , but a in the 1st case (with type double) isn't.

A core constant expression is any expression whose evaluation would not evaluate any one of the following:

    1. an lvalue-to-rvalue implicit conversion unless....

      • a. applied to a non-volatile glvalue that designates an object that is usable in constant expressions (see below),

        int main() {
            const std::size_t tabsize = 50;
            int tab[tabsize]; // OK: tabsize is a constant expression
                              // because tabsize is usable in constant expressions
                              // because it has const-qualified integral type, and
                              // its initializer is a constant initializer
        
            std::size_t n = 50;
            const std::size_t sz = n;
            int tab2[sz]; // error: sz is not a constant expression
                          // because sz is not usable in constant expressions
                          // because its initializer was not a constant initializer
        }
        

and

(emphasis mine)

Usable in constant expressions In the list above, a variable is usable in constant expressions if it is

  • a constexpr variable, or
  • it is a constant-initialized variable
    • of reference type or
    • of const-qualified integral or enumeration type.

You can declare a as constexpr to make it usable in constant expression. e.g.

constexpr double a = 1.0;
constexpr double b = a;   // works fine now
like image 77
songyuanyao Avatar answered Nov 16 '22 12:11

songyuanyao