C++ 传递 typedef

标签 c++

我想知道如何将 typedef 传递给函数。例如:

    typedef int box[3][3];
    box empty, *board[3][3];

我如何将 board 传递给函数?我还可以在函数参数内部使用 decltype() 吗?

最佳答案

你会这样做:

using box = std::array<std::array<int, 3>, 3>;

然后:

void fn(box const& x)
void fn(box& x)
void fn(box&& x)

或者你需要的任何东西。

是的,您可以在函数中使用 decltype

作为一个实际的例子,您可以定义一个打印盒子内容的函数:

using box = std::array<std::array<int, 3>, 3>;

void fn(box const& arr) {
    for (auto const& x : arr) {
        for (auto i : x) {
            std::cout << i << ' ';
        }
        std::cout << '\n';
    }
}

然后调用它:

int main() {
    box x {{
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    }};
    fn(x);
}

Live demo

关于C++ 传递 typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38921293/

相关文章:

c++ - 将 static const char[] 设置为预定义的 static const char[] 失败

c++ - 检测 32 位双字 + 双字进位/C++

c++ - CUBLAS 矩阵乘法与行主数据无转置

c++ - 编译时如何处理 C++ 解决方案源文件?

c++ - 如何检查一个结构是否有某个变量?

c++ - 为什么我们需要在 C++ 中显式声明指针类型?

c++ - 打印所有可能的结果 C++?

c++ - 包含指向成员的指针的内联成员初始值设定项

c++ - OpenGL 属性偏移量未按预期工作

c++ - std::string 和数据对齐