Note that there are some explanatory texts on larger screens.

plurals
  1. POType trait to obtain default argument promotions
    primarykey
    data
    text
    <p>[Disclaimer: I know an answer to this question. I thought it might be of some general interest.]</p> <p><strong>Question:</strong> How can we have a type trait that produces the type that results from performing <em>default argument promotions</em>?</p> <p><strong>Motivation:</strong> I would like to be able to use variable arguments portably. For example:</p> <pre><code>void foo(char const * fmt, ...); // Please pass: * unsigned short // * bool // * char32_t // * unsigned char </code></pre> <p>When passing arguments to a function call without parameters, i.e. matching the ellipsis, the arguments undergo default argument promotion. So far so good, but those promotions are platform dependent. I can recover the arguments with <code>va_arg(ap, T)</code>, but what is <code>T</code>?</p> <p>Now, for some simple situations this is easy: For example, I can always say:</p> <pre><code>unsigned short n = va_args(ap, unsigned int); </code></pre> <p>The default promotion will result in either a <code>signed int</code> or an <code>unsigned int</code>, but according to, say, C11 7.16.1.1/3, va-casting to <code>unsigned int</code> is always fine, since even if the default promotion results in an <code>int</code>, the original value can be represented by both types.</p> <p>But what type should I cast to when I expect a <code>char32_t</code>? C++11 4.5/2 leaves the resulting type wide open. So I would like a trait that lets me write:</p> <pre><code>char32_t c = va_args(ap, default_promote&lt;char32_t&gt;::type); </code></pre> <p>How to do this?</p> <p>Bonus points for a trait that produces a static assertion when the parameter type must not be passed as a variable argument.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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