Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No. The return type of a C++ function can only vary based on explicit template parameters or the <em>types</em> of its arguments. It cannot vary based on the <em>value</em> of its arguments.</p> <p>However, you can use various techniques to create a type that is the union of several other types. Unfortunately this won't necessarily help you here, as one such technique is void * itself, and getting back to the original type will be a pain.</p> <p>However, by turning the problem inside out you may get what you want. I imagine you'd want to use the code you posted as something like, for example:</p> <pre><code>void bitmap_operation(void *data, int depth, int width, int height) { some_magical_type p_pixels = returnPointer(data, depth); for (int x = 0; x &lt; width; x++) for (int y = 0; y &lt; width; y++) p_pixels[y*width+x] = some_operation(p_pixels[y*width+x]); } </code></pre> <p>Because C++ needs to know the type of p_pixels at compile time, this won't work as-is. But what we can do is make bitmap_operation itself be a template, then wrap it with a switch based on the depth:</p> <pre><code>template&lt;typename PixelType&gt; void bitmap_operation_impl(void *data, int width, int height) { PixelType *p_pixels = (PixelType *)data; for (int x = 0; x &lt; width; x++) for (int y = 0; y &lt; width; y++) p_pixels[y*width+x] = some_operation(p_pixels[y*width+x]); } void bitmap_operation(void *data, int depth, int width, int height) { if (depth == 8) bitmap_operation_impl&lt;uint8_t&gt;(data, width, height); else if (depth == 16) bitmap_operation_impl&lt;uint16_t&gt;(data, width, height); else if (depth == 32) bitmap_operation_impl&lt;uint32_t&gt;(data, width, height); else assert(!"Impossible depth!"); } </code></pre> <p>Now the compiler will automatically generate three implementations for bitmap_operation_impl for you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload