Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
"Unpacking" macro arguments

I have the following macro:

#define HEX 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00

#define BITS 0x01

#define ADD_FLAGS(a, b, c, d, e, f, g, h) \
        a, b | BITS, c, d, e, f, g, h

I use this somewhere to create a byte array {ADD_FLAGS(HEX)}. This doesn't work. HEX is interpreted as a single argument to ADD_FLAGS and I get errors that there are too few arguments.

const myStructure Table[ENTRY_COUNT] =                   
{
   /* Entity Index 0 */
   {
      <some structure fields>,
      ADD_FLAGS(HEX),     // 8-byte array
      <more structure fields>
   }
}

I have also tried the following variants, but no success:

#define EXPAND(x) x
#define ADD_FL(a, b, c, d, e, f, g, h) \
        a, b | BITS, c, d, e, f, g, h
#define ADD_FLAGS(...) ADD_FL EXPAND((__VA_ARGS__))

##################### other variant
#define ADD_FLAGS(...) EXPAND(ADD_FL (__VA_ARGS__))

I am using C99 and three different compilers, so any answer has to be standards compliant, otherwise there is surely going to be a comiler that's not working (I am using armclang, ghs and tasking).

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Obligatory "avoid macros" aside, you just need to wrap ADD_FLAGS in another macro.

Rename it to something else, e.g. ADD_FLAGS_, and add

#define ADD_FLAGS(...) ADD_FLAGS_(__VA_ARGS__)

This works in GCC and Clang. In MSVC this works with the new preprocessor only (/Zc:preprocessor flag).

Not sure how to do it with the old MSVC preprocessor, but since it's notoriously buggy, I'd avoid supporting it if possible.

over 4 years ago · Santiago Trujillo Relatório

0

I would suggest that you use macros sparingly. Here's an example snippet which does exactly what the macro is doing but it's a bit more reasonable in my opinion.

template<typename ... Ts>
constexpr auto add_flags(Ts && ... args){
     std::array flags{std::forward<Ts>(args)...};
     constexpr int bits = 0x01;
     // flags[some_idx] |= bits;
     return flags;
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda