我有一个采用多维的函数,std::vector并需要将深度(或维数)作为模板参数传入。我不想对这个值进行硬编码,我想编写一个constexpr函数,它将std::vector深度作为unsigned integer值返回。
例如:
std::vector<std::vector<std::vector<int>>> v =
{
{ { 0, 1}, { 2, 3 } },
{ { 4, 5}, { 6, 7 } },
};
// Returns 3
size_t depth = GetDepth(v);
Run Code Online (Sandbox Code Playgroud)
这需要在编译时完成,因为这个深度将作为模板参数传递给模板函数:
// Same as calling foo<3>(v);
foo<GetDepth(v)>(v);
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?