I wonder if the original array of length can be converted to std::initializer_list.

struct stc {
    int arr[1]; // flexible array member is not supported in CPP
};

void test(std::initializer_list<int> lst) {
    for (auto l : lst)
        std::cout << l << std::endl;
}

int main() {
    stc s;
    for (int i = 0; i < 10; PPi) {
        s.arr[i] = i;
    }
    // 10(s.arr)
    // test()s.arr
}
CPP
Mar.15,2021

The

CPP standard only provides that the compiler can help us convert a constant array defined by {} into an initializer_list object, so that functions that accept initializer_list as parameters (usually various constructors) can accept {}, which extends the syntax so that we can initialize the container like a normal array, simplifying the code.

but don't use initializer_list as a normal (container).

in the implementation version of GCC (4.8), the constructor of initializer_list is implemented as private. You cannot construct a valid initializer_list except through {} and ask the compiler to do it for you.

namespace std
{
  /// initializer_list
  template<class _E>
    class initializer_list
    {
    public:
      typedef _E                value_type;
      typedef const _E&         reference;
      typedef const _E&         const_reference;
      typedef size_t            size_type;
      typedef const _E*         iterator;
      typedef const _E*         const_iterator;

    private:
      iterator                  _M_array;
      size_type                 _M_len;

      // The compiler can call a private constructor.
      constexpr initializer_list(const_iterator __a, size_type __l)
      : _M_array(__a), _M_len(__l) { }

    public:
"/usr/include/cPP/4.8/initializer_list" [readonly] 107 lines --17%--                   

in the implementation version of VCPP 2017, the constructor of initializer_list is public.

constexpr initializer_list(const _Elem *_First_arg,
        const _Elem *_Last_arg) _NOEXCEPT
        : _First(_First_arg), _Last(_Last_arg)
        {    // construct with pointers
        }

so the following code is ok on VCPP2017:

-sharpinclude <iostream>

void test(std::initializer_list<int> lst) {
    for (auto l : lst)
        std::cout << l << std::endl;
}

int main() {
    int a[] = { 1, 2, 3, 4 };

    test({ 1, 2, 3, 4 }); // cPP standard 

    test(std::initializer_list<int>(a, a + 4)); // compile error in gcc 4.8 but ok in vcPP2017

    return 0;
}

but this is not guaranteed.

CPP supports a new syntax through library code in conjunction with the compiler, which may be the cause of confusion.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e48b98-652f2.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e48b98-652f2.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?