warning: array 'alphabet' initialized by parenthesized string literal

arrays, c++, c++11, initialization, string

Solution

The simplest way to make your code unambiguously valid is to use a reference to an array:

static const char (&alphabet)[17] = 
    (Uppercase) ? ("0123456789ABCDEF") : ("0123456789abcdef");

This doesn't rely on the special exception that allows a string literal to be used for array initialisation. It does, as noted by neverhoodboy, require that you deal with an array of `char`, rather than an array of `unsigned char`.

You could still use this if you really need an array of `unsigned char` by using extra helper variables:

static const unsigned char uppercase[17] = "0123456789ABCDEF";
static const unsigned char lowercase[17] = "0123456789abcdef";
static const unsigned char (&alphabet)[17] =
    (Uppercase) ? uppercase : lowercase;

Note: when `Uppercase` is known at compile-time (you say it is a template parameter, so it should be), you may also add the `constexpr` keyword.

Problem

In a templated function, I currently have the following line: ``` static const unsigned char alphabet[17] = (Uppercase) ? ("0123456789ABCDEF") : ("0123456789abcdef"); ``` Where `Uppercase` is a template parameter. With `-pedantic` gcc tells me: ``` warning: array 'alphabet' initialized by parenthesized string literal '("0123456789abcdef")' ``` How to get rid from that message (I want the `alphabet` to be in the stack) ?

Original source