The constructor of a derived class cannot be constexpr if the base class contains an array element

I want to determine the constexpr values ​​of a derived type (SBar) using a constructor whose only argument is a base class variable (SFoo), which is simply used to initialize the database.

This works great when the base class does not have an array element. However, when I add an array, derived values ​​can no longer be constexpr. However, a simple copy of the base class gives constexpr results.

I explicitly refused all copy and move constructors to be safe.

test.cpp

#define USE_ARRAY


struct SFoo
    {
        constexpr SFoo() =default;
        constexpr SFoo(SFoo const&) =default;
        constexpr SFoo(SFoo      &) =default;
        constexpr SFoo(SFoo     &&) =default;
        constexpr SFoo& operator = (SFoo const&) =default;
        constexpr SFoo& operator = (SFoo      &) =default;
        constexpr SFoo& operator = (SFoo     &&) =default;

#       ifdef USE_ARRAY
            constexpr SFoo(int const (&array)[1]) :
                M_array{array[0]}
                {}

            int M_array[1] = {0};
#       else
            constexpr SFoo(int value) :
                M_value{value}
                {}

            int M_value = 0;
#       endif
    };


struct SBar : SFoo
    {
        constexpr SBar() =default;
        constexpr SBar(SBar const&) =default;
        constexpr SBar(SBar      &) =default;
        constexpr SBar(SBar     &&) =default;
        constexpr SBar& operator = (SBar const&) =default;
        constexpr SBar& operator = (SBar      &) =default;
        constexpr SBar& operator = (SBar     &&) =default;

        constexpr SBar(SFoo foo) : SFoo(foo) {}
    };


// Instances:

#       ifdef USE_ARRAY
    constexpr int arg[1] = {3};
#       else
    constexpr int arg = 3;
#       endif

constexpr SFoo foo(arg); // base "value" constructor is constexpr.
constexpr SFoo foo2(foo); // base copy constructor is constexpr.
constexpr SBar bar(foo); // (line 54): this line fails.

compilation with

clang++ -std=c++1z -c -o test.o test.cpp 

gives

test.cpp:54:16: error: constexpr variable 'bar' must be initialized by a constant expression
constexpr SBar bar(foo);
               ^~~~~~~~
1 error generated.

however, everything works if I do not define USE_ARRAY.

Does anyone know why this is happening?

( , std:: array , ).

+4
2

, clang . :

constexpr SBar(SFoo foo) : SFoo(foo) {}

foo const:

constexpr SBar(const SFoo &info) : SFoo(info) {}

, , , , - sFoo:

//constexpr SFoo(SFoo      &) =default;

++ 1z, .

, gcc , , constexpr ( ), :

error: explicitly defaulted function 'constexpr SFoo& SFoo::operator=(const SFoo&)' cannot be declared as constexpr because the implicit declaration is not constexpr
    constexpr SFoo& operator = (SFoo const&) =default;
                    ^

7.1.5 [dcl.constexpr] 5.20 [expr.const].

12.8p26, / constexpr. gcc .

+1

- . . , CLANG, GCC:

struct SFoo {
  constexpr SFoo() = default;
  constexpr SFoo(SFoo const&) = default;
  constexpr SFoo(SFoo&&) = default;
  constexpr SFoo(int const (&array)[1]) : M_array{array[0]} {}

  int M_array[1] = {0};
};

struct SBar : SFoo {
  constexpr SBar() = default;
  constexpr SBar(SBar const&) = default;
  constexpr SBar(SBar&&) = default;
  constexpr SBar(SFoo info) : SFoo(info) {}
};

Live Demo

, std::array:

struct SFoo {
  constexpr SFoo() = default;
  constexpr SFoo(SFoo const&) = default;
  constexpr SFoo(SFoo      &) = default;
  constexpr SFoo(SFoo     &&) = default;
  constexpr SFoo& operator = (SFoo const&) = default;
  constexpr SFoo& operator = (SFoo      &) = default;
  constexpr SFoo& operator = (SFoo     &&) = default;
  constexpr SFoo(std::array<int, 1> const &array) : M_array{array} {}

  std::array<int, 1> M_array = {};
};

Live Demo

, . ...

0

Source: https://habr.com/ru/post/1618647/


All Articles