是否可以将固定大小的数组作为函数参数就地构造?

alf*_*lfC 2 c++ arrays rvalue temporary-objects c++11

这主要是关于与固定大小的数组相关的C++语法的一个问题.

假设我有一个利用类型信息的函数,例如:

template<class T> void fun(T const& t){
    std::cout << typeid(t).name() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我可以传递一个值或一个临时对象:

int i;
fun(i); // prints "int" ("i" actually)
fun(int{});   // prints "int" ("i" actually)
Run Code Online (Sandbox Code Playgroud)

但是我不能对数组做同样的事情

double a[10][10];
fun(a); // ok, prints "a[10][10]" ("A10_A10_d" actually)

fun(double[10][10]); // doesn't compile
fun(double{}[10][10]); // doesn't compile
fun(double[10][10]{}); // doesn't compile
fun(double()[10][10]); // doesn't compile
fun(double[10][10]()); // doesn't compile
fun(double(&)[10][10]); // doesn't compile
fun(double(*)[10][10]); // doesn't compile
Run Code Online (Sandbox Code Playgroud)

我原则上可以这样做:

typedef double a1010[10][10];
fun(a1010{});
Run Code Online (Sandbox Code Playgroud)

但是,有没有预先定义一个typedef呢?

是否有可能将一个固定大小的数组作为函数参数就地构造?

完整代码:

template<class T> void fun(T const& t){
    std::cout << typeid(t).name() << std::endl;
}

typedef double a1010[10][10];

int main(){
    int i;
    fun(i); // prints "int" ("i" actually)
    double a[10][10];
    fun(a); // prints "a[10][10]" ("A10_A10_d" actually)
    fun(a1010{});

    fun(int{});   // prints "int"
/*  fun(double[10][10]); // doesn't compile
    fun(double{}[10][10]); // doesn't compile
    fun(double[10][10]{}); // doesn't compile
    fun(double()[10][10]); // doesn't compile
    fun(double[10][10]()); // doesn't compile
    fun(double(&)[10][10]); // doesn't compile
    fun(double(*)[10][10]); // doesn't compile
    */
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

奖励积分(可能是赏金):可变大小的阵列怎么样?

int N = 10;
f(double[N]);
Run Code Online (Sandbox Code Playgroud)

Mic*_*ler 5

尝试:

fun((int[3]){1,2,3});
fun((int[5]){});
Run Code Online (Sandbox Code Playgroud)

至于"奖励积分":可变大小的数组不是语言的一部分.此语言的扩展名不适用于模板参数:

prog.cc:4:6:注意:候选模板被忽略:替换失败:可变修改类型'int [n]'不能用作模板参数fun(const T&t)

编辑

正如Chris所指出的,上述解决方案建议使用复合文字,它是C++的扩展.有一个解决方案,使用一个简单的帮助器类来避免对C++的这种扩展:

template <class T, std::size_t N>
struct my_array
{
    T data[N];
};

template <class T, std::size_t N>
void print(const T (&x)[N])
{
     for (auto i: x)
         std::cout << i << '\n';
}

int main()
{
    print(my_array<int,3>{9,10,11}.data);
}
Run Code Online (Sandbox Code Playgroud)

这种方法效果很好,但需要向my_array添加模板参数,这些参数不是推导出来的.使用C++ 17,可以自动推断类型和大小:

template <class T, std::size_t N>
struct my_array
{
    constexpr my_array(std::initializer_list<T> x)
    {
       std::size_t i = 0;
       for (auto val : x)
           data[i++] = val;
    }
    T data[N];
};
template <class ...T>
my_array(T...) -> my_array<typename std::common_type<T...>::type, sizeof...(T)>;

int main()
{
    print(my_array{9,10,11}.data);
}
Run Code Online (Sandbox Code Playgroud)

对于二维数组,这稍微复杂一些:

template <class T, std::size_t N1, std::size_t N2>
struct my_array2d
{
    constexpr my_array2d(std::initializer_list<std::initializer_list<T> > x)
    {
        std::size_t i = 0;
        for (const auto & row : x) {
            int j=0;
            for (const auto & val: row) {
                data[i][j++] = val;
            }
            i++;
        }
    }
    T data[N1][N2];
};
int main()
{
    work(my_array2d<int, 3, 2>{{9,1},{10,2},{11,3}}.data);
}
Run Code Online (Sandbox Code Playgroud)

我已经放弃了二维阵列的演绎指南,但我相信它们是可能的.