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

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

我如何将板子传递到功能上?同样在函数参数中,我可以使用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);
}

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

10-15 17:56