invalid initializer for structured binding declaration

How should I fix this error? Should I use a const std::tuple<int, int, char> instead?

constexpr auto [ Y_AxisLen, X_AxisLen, fillCharacter ] { 36, 168, ' ' };

This gives errors like:

error: structured binding declaration cannot be 'constexpr'
  434 |         constexpr auto [ Y_AxisLen, X_AxisLen, fillCharacter ] { 36, 168, ' ' };

 error: invalid initializer for structured binding declaration
  434 |         constexpr auto [ Y_AxisLen, X_AxisLen, fillCharacter ] { 36, 168, ' ' };

Y_AxisLen and X_AxisLen should be of type int and fillCharacter should be a char.

>Solution :

  • structure binding isn’t allowed to be constexpr now.
  • structure binding can only be applied to the following cases.
    • array
    • tuple-like
    • structure data members

ref: https://en.cppreference.com/w/cpp/language/structured_binding

If you must use structure binding, use std::make_tuple

const auto [ Y_AxisLen, X_AxisLen, fillCharacter ] = std::make_tuple(36, 168, ' ');

Leave a Reply