title: GCC transparent union
body: <p>So I have a union defined like:</p>
  
  <pre><code>typedef union
  {
     uint8_t  *b1;
     uint16_t *b2;
     uint32_t *b3;
     uint64_t *b4;
  } buffer_u __attribute__((__transparent_union__));
  </code></pre>
  
  <p>And a functions defined like:</p>
  
  <pre><code>int write_byte_to_buffer(buffer_u buf, uint16_t length, uint8_t val);
  int write_short_to_buffer(buffer_u buf, uint16_t length, uint16_t val);
  int write_int_to_buffer(buffer_u buf, uint16_t length, uint32_t val);
  int write_long_long_to_buffer(buffer_u buf, uint16_t length, uint64_t val);
  </code></pre>
  
  <p>Then I am invoking it like:</p>
  
  <pre><code>uint8_t buffer[1024];
  write_long_long_to_buffer(buffer, 1024, some_value);
  </code></pre>
  
  <p>The compiler gives me the following error:</p>
  
  <pre><code>error: conversion from ‘uint8_t*’ to non-scalar type ‘buffer_u’ requested
  </code></pre>
  
  <p>Why am I getting this error? I must be reading the documentation on the <strong>transparent_union</strong> attribute incorrectly because I would expect any of the union member types to be able to be passed in where a buffer_u is expected.</p>
  
  <p>I am trying to avoid using a void * for the buffer type, or different pointer types for each function, but may have to fall back to that.</p>
  
  <p>One more useful bit of information. The code is C, but the code using the library is C++.</p>
  
  <p>Is there something special about C++ that doesn't allow this?</p>