[英]std::array with unknown length as a type for template function specification
I want to pass std::array
class with a known type, but unknown size to the template function specialization: 我想将具有已知类型但大小未知的
std::array
类传递给模板函数专门化:
class foo {
public:
void bar<class T>(T arg1, int arg2, int arg3) {
//For any other T
}
void bar<std::array<rs::float3, size>>(T arg1, arg2) {
//Specific function for type std::array of rs::float3's and unknown length (size is not a type, just variable).
}
What about something like 怎么样
template <typename T>
void foo (T const &)
{ std::cout << "foo generic" << std::endl; }
template <std::size_t Dim>
void foo (std::array<float, Dim> const &)
{ std::cout << "foo float array dim " << Dim << std::endl; }
? ?
The following is a full working example 以下是完整的工作示例
#include <array>
#include <iostream>
template <typename T>
void foo (T const &)
{ std::cout << "foo generic" << std::endl; }
template <std::size_t Dim>
void foo (std::array<float, Dim> const &)
{ std::cout << "foo floar array dim " << Dim << std::endl; }
int main ()
{
foo(0);
foo(std::array<float, 12U>{});
}
If you want the function to accept array
objects of different sizes, you pretty much need to make it a function template, and pass the size as a template parameter: 如果要让函数接受不同大小的
array
对象,则非常需要使其成为函数模板,并将大小作为模板参数传递:
template<size_t N>
void bar(std::array<rs::float3, N> const &arg1,
std::array<rs::float3, N> cons t&arg2)
{
// ...
}
In this case, I guess you also want it to be a partial specialization of another template. 在这种情况下,我想您也希望它是另一个模板的部分专业化。 Unfortunately, there is no partial specialization of function templates.
不幸的是,功能模板没有部分专业化。 There is a part of the standard that deals with partial ordering of function templates, but the usual recommendation is that you just use overloading instead.
标准的一部分涉及功能模板的部分排序,但是通常的建议是您只使用重载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.