Skip to content

Can't use sizeof... to initialize struct template members #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Ebola-Chan-bot opened this issue Feb 22, 2025 · 5 comments
Open

Can't use sizeof... to initialize struct template members #151

Ebola-Chan-bot opened this issue Feb 22, 2025 · 5 comments

Comments

@Ebola-Chan-bot
Copy link

#include<stdint.h>
template<typename... Ts>
struct InfoArray {
  uint8_t Number = sizeof...(Ts);
};

The code above does not compile.

C:\Users\vhtmf\Documents\Arduino\sketch_nov5a\sketch_nov5a.ino:4:20: error: expected ';' at end of member declaration
   uint8_t Number = sizeof...(Ts);
                    ^
C:\Users\vhtmf\Documents\Arduino\sketch_nov5a\sketch_nov5a.ino:4:26: error: expected unqualified-id before '...' token
   uint8_t Number = sizeof...(Ts);
                          ^
C:\Users\vhtmf\Documents\Arduino\sketch_nov5a\sketch_nov5a.ino:4:20: error: expected primary-expression at end of input
   uint8_t Number = sizeof...(Ts);
                    ^

exit status 1

Compilation error: expected ';' at end of member declaration
@christyogi
Copy link

Use constexpr static uint8_t Number = sizeof...(Ts);

@Ebola-Chan-bot
Copy link
Author

Use constexpr static uint8_t Number = sizeof...(Ts);

Your code doesn't have the same meaning as mine. What I need must be a non-static member variable.

@christyogi
Copy link

This maybe?

#include<stdint.h>
template<typename... Ts>
struct InfoArray {
  uint8_t Number;
  InfoArray() : Number{sizeof...(Ts)} {};
};

@christyogi
Copy link

Just doing

  uint8_t Number = {sizeof...(Ts)};

seems fine in fact

@Ebola-Chan-bot
Copy link
Author

Just doing

  uint8_t Number = {sizeof...(Ts)};

seems fine in fact

This is exactly what I did as a workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants