1

Consider the following function which takes a reference to an array of a size N deduced at compile time:

template <size_t N>
void array_consumer(unsigned const (&source)[N]) {
  // ...
}

I want to call it with an array initializer like this:

array_consumer({1u, 2u, 3u, 4u});

This works, but I don't want to specify the u after every value (the actual arrays may be much longer). Is there something I can do to the function signature or at the call site to avoid this?

Note that I need the array to actually be of unsigned type inside array_consumer (and I don't really want to make a copy) so changing the signature to int const (&source)[N]) doesn't work.

3
  • 1
    There should be no need to specify u after every value. I assume you are using GCC as that seems to be the only compiler I could test that would complain about this. IMHO this must be a compilerbug as [dcl.init.list] § 7.4 should explicitly allow this. Seems to be fixed in current trunk: godbolt.org/g/5gNwWn. The question is, of course, if updating or changing compilers is an option for you… Commented Jul 17, 2018 at 2:00
  • @MichaelKenzel - yeah, I'm using gcc 5.5, it hadn't occurred to me it might be a compiler bug! Changing compilers is not in the cards for a "minor irritation" like this though. Commented Jul 17, 2018 at 2:56
  • A pragmatic (text editor) solution: Write down your array initializer without u suffixes. Then select the whole initializer and replace , by u, (and fix the last value manually). Commented Jul 17, 2018 at 6:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.