static auto constexpr t = {"red", "black", "green"}; 是什么类型的?派生到?

What type does static auto constexpr t = {"red", "black", "green"}; derive to?

所以我在弄乱一些代码,我决定我想要某种字符串列表...所以我想 - 我懒得弄清楚要使用什么类型等等,所以我会把它打成一个自动变量,像这样:

static auto constexpr t = {"red", "black", "green"};

代码已编译。太好了,所以自从这个:

static auto constexpr str = "green";

派生到 char[] 我假设 {"red", "black", "green"} 可能是 *char[] 或类似的东西,因此我可以写:

std::cout << "value 1 is: " << t[1] << std::endl;

这给了我错误:

main.cpp:18:56: error: no match for ‘operator[]’ (operand types are ‘const std::initializer_list’ and ‘int’)

所以我假设类型是 "initializer_list"?有没有办法我可以做类似的事情: t[1] 得到字符串 "black" (假设索引从 0 开始)?

So I presume the type is "initializer_list"?

是的。

Is there a way I can do somthing like: t[1] to get at the string "black" (assuming index starts at 0)?

使用 std::array,它与 C++17 class 模板类型推导相得益彰:

static auto constexpr t = std::array{"red", "black", "green"};

std::cout << t[1] << "\n"; // outputs 'black'

constexpr-ness 如果将编译时常量传递给 operator[] or std::get.

,则会保留
constexpr const char* black = t[1];
constexpr const char* red = std::get<0>(t);

An object of type std::initializer_list is a lightweight proxy object that provides access to an array of objects of type const T. (https://en.cppreference.com/w/cpp/utility/initializer_list)

它没有 operator[] 成员函数,但是,唯一的访问器是 std::initializer_list::beginstd::initializer_list::end。例如:

#include <iostream>
#include <initializer_list>
#include <stdexcept>

namespace my {

template <typename T>
constexpr auto get(std::initializer_list<T> lst, std::size_t i)
{
    if ( i >= lst.size() )
        throw std::out_of_range("Out of bounds access.\n");
    return *(lst.begin() + i);   
}

template <std::size_t I, typename T>
constexpr auto get(std::initializer_list<T> lst) 
{
    // Note: 
    if ( I >= lst.size() )
        throw std::out_of_range("Out of bounds access.\n");
    return *(lst.begin() + I);   
}

}

int main()
{
    // Note: 
    static constexpr auto t = {"red", "black", "green"};

    std::cout << my::get(t, 1) << '\n' << my::get<2>(t) << '\n';
}