Home > Back-end >  invalid initializer for structured binding declaration
invalid initializer for structured binding declaration

Time:12-09

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.

CodePudding user response:

  • 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, ' ');
  • Related