Warum ist dies nicht ein konstanter Ausdruck?

In diesem trivialen Beispiel test2 nicht kompiliert werden, obwohl test1 gelingt, und ich sehe nicht, warum das der Fall ist. Wenn arr[i] geeignet ist für einen Rückgabewert aus einer Funktion markiert constexpr kann, warum kann es nicht verwendet werden als non-type template argument?

template<char c>
struct t
{ 
    static const char value = c;
};

template <unsigned N>
constexpr char test1(const char (&arr)[N], unsigned i)
{
    return arr[i];
}

template <unsigned N>
constexpr char test2(const char (&arr)[N], unsigned i)
{
    return t<arr[i]>::value;
}

int main()
{
   char a = test1("Test", 0); //Compiles OK
   char b = test2("Test", 0); //error: non-type template argument 
                              //is not a constant expression
}

Edit: Das macht keinen Unterschied:

template<char c>
struct t
{ 
    static const char value = c;
};

template <unsigned N>
constexpr char test1(const char (&arr)[N])
{
    return arr[0];
}

template <unsigned N>
constexpr char test2(const char (&arr)[N])
{
    return t<arr[0]>::value;
}

int main()
{
   char a = test1("Test"); //Compiles OK
   char b = test2("Test"); //error: non-type template argument 
                           //is not a constant expression
}

InformationsquelleAutor der Frage Chris_F | 2014-07-04

Schreibe einen Kommentar