Is this code legal?

Sergey Sadovnikov flex_ferrum@artberg.ru
Mon Oct 5 19:29:00 GMT 2009


Hello all.

I try to compile the following code sample:

(with MinGW gcc version 4.5.0 20090819 (experimental) (GCC))

#include <iostream>
#include <algorithm>
#include <array>

template<wchar_t... Chars>
struct Message
{
    std::array<wchar_t, sizeof ... (Chars)> m_Message;
    
    Message() : m_Message{Chars...}             // {*1}
    {
                std::array<wchar_t, sizeof ... (Chars)> msg = {Chars ... };     // {*2}
    }
};

int main(int argc, char* argv[])
{
    setlocale(LC_ALL, ".OCP");
        
    Message<L'\x421', L'\x20', L'\x434', L'\x43D', L'\x451', L'\x43C', L'\x20', L'\x43F', L'\x440', L'\x43E',
            L'\x433', L'\x440', L'\x430', L'\x43C', L'\x43C', L'\x438', L'\x441', L'\x442', L'\x430', L'\x2C', L'\x20', L'\x43A',
            L'\x43E', L'\x43B', L'\x43B', L'\x435', L'\x433', L'\x438', L'\x21', L'\n'
            > msg;
            
    std::for_each(msg.m_Message.begin(), msg.m_Message.end(), [](wchar_t c){std::wcout << c;});
            
    return 0;
}

Can anybody explain why line marked with '{*1}' produce this error
message:

hello_programmer.cpp: In constructor 'Message<Chars>::Message() [with wchar_t ...Chars = 1057u, 32u, 1076u, 1085u, 1105u
, 1084u, 32u, 1087u, 1088u, 1086u, 1075u, 1088u, 1072u, 1084u, 1084u, 1080u, 1089u, 1090u, 1072u, 44u, 32u, 1082u, 1086u
, 1083u, 1083u, 1077u, 1075u, 1080u, 33u, 10u]':
hello_programmer.cpp:48:15:   instantiated from here
hello_programmer.cpp:34:35: error: could not convert '{1057u, 32u, 1076u, 1085u, 1105u, 1084u, 32u, 1087u, 1088u, 1086u,
 1075u, 1088u, 1072u, 1084u, 1084u, 1080u, 1089u, 1090u, 1072u, 44u, 32u, 1082u, 1086u, 1083u, 1083u, 1077u, 1075u, 1080
u, 33u, 10u}' to 'std::array<wchar_t, 30u>'

Line marked with '{*2}' is compiled successfully.

Or there is no standard-conforming way to initialize std::array
within constructor initializer list?

-------------
Best regards, Sergey.



More information about the Gcc mailing list