简体   繁体   English

长度未知的std :: array作为模板功能规范的类型

[英]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. 标准的一部分涉及功能模板的部分排序,但是通常的建议是您只使用重载。

Try this: 尝试这个:

class foo
{
public:
    template <class T>
    void bar(T arg1, int arg2, int arg3) {
        //For any other T
    }

    template <size_t size>
    void bar(std::array<rs::float3, size> arg1, int arg2) { 
        //Specific function for type std::array of rs::float3's of caller-specified length
    }
};

Live demo 现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有明确类型说明的功能模板 - Function template with explicit specification of type 将模板(没有规范)传递给 std::thread() 会产生错误:<unresolved overloaded function type> 匹配错误</unresolved> - passing template (without a specification) to an std::thread() gives error : <unresolved overloaded function type> matching Error 将未知类型和大小的std :: array传递给模板函数的构造函数 - Passing a std::array of unknown type and size to a constructor of a templated function 具有未知返回类型的Std函数 - Std function with unknown return type std::array 模板 function - std::array template function std :: function type和template instantiation - std::function type and template instantiation 使用std :: function的模板类型推导 - Template type deduction with std::function 错误C2893无法专门化函数模板&#39;unknown-type std :: invoke(_Callable &amp;&amp;,_ Types &amp;&amp; ...)&#39; - Error C2893 Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&…)' 在模板函数的声明中,`decltype`作为模板类型规范的一部分 - `decltype` as part of template type specification within declaration of a template function 如果模板是 integer 类型,则仅对某些模板规范启用 function - Enable function only for some template specification if template is of integer type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM