0

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.

digito_evo
  • 3,216
  • 2
  • 14
  • 42
  • Well the first error message is pretty clear, you can't use `constexpr` here. Why do you need these values to be `constexpr`? Perhaps it's enough to make them `const`? – Some programmer dude Dec 09 '21 at 08:24
  • @Some programmer dude Switching to `const` won't make a difference. Only the first error will disappear. – digito_evo Dec 09 '21 at 08:26
  • 1
    And why do you need structured binding here instead of e.g. plain and simple `constexpr int Y_AxisLen = 36, X_AxisLen = 168; constexpr char fillCharacter = ' ';`? Don't overthink your code, or use new features just because they're new. :) – Some programmer dude Dec 09 '21 at 08:28
  • @Some programmer dude Yeah, that's a valid choice. I just wanted the code to be a little bit more compact. – digito_evo Dec 09 '21 at 08:29
  • 1
    Compactness is not a goal for any project. Whether you write this or the long variant, you can be the compiler will generate the same exact thing. – lionkor Dec 09 '21 at 08:31
  • Brevity is usually not a good goal in programming. Readability, the ability to clearly convey meaning, and maintainability are much more important. :) – Some programmer dude Dec 09 '21 at 08:32

1 Answers1

3
  • 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, ' ');
Nimrod
  • 2,908
  • 9
  • 20