简体   繁体   English

使用 STL 的部分模板特化

[英]Partial template specialization using STL

I have a function which has template class object as parameter.我有一个 function 模板 class object 作为参数。 Its working fine with user defined template class object as paramerter but its giving arror with std::array object.它使用用户定义的模板 class object 作为参数可以正常工作,但它使用 std::array object 给出了错误。 If I am trying to execute this program如果我试图执行这个程序

#include<iostream>
#include<array>

template<typename T1, int size>
void print(std::array<T1, size> &arr)
{
    for (auto ele : arr)
        std::cout << ele << " ";
}

template<int size>
void print(std::array<float, size> &arr)
{
    std::cout << "its float" << std::endl;
    for (auto ele : arr)
        std::cout << ele << " ";
}

int main()
{
    std::array<int, 3> arr{1,3,4};
    std::array<float, 2> ar2{1.3, 3.4};
    print(arr);
    print(ar2);
    return 0;
}

And getting this error并得到这个错误

error: no matching function for call to ‘print(std::array<int, 3>&)’
     print(arr);

But when I am running this program then its working fine.但是当我运行这个程序时,它工作正常。

#include<iostream>
#include<array>

template<class T, int size>
class Template
{
    public:
    std::array<T, size> arr;
};

template<typename T1, int size>
void print(Template<T1, size> &obj)
{
    for (auto ele : obj.arr)
        std::cout << ele << " ";
}

template<int size>
void print(Template<float, size> &obj)
{
    for (auto ele : obj.arr)
        std::cout << ele << " ";
}

int main()
{
    Template<int, 3> array;
    array.arr[0] = 1;
    array.arr[1] = 2;
    array.arr[2] = 3;
    Template<float, 2> arr2;
    arr2.arr[0] = 2.3;
    arr2.arr[1] = 2.9;
    print(array);
    print(arr2);
    return 0;
}

I don't understand why std::array<T1, size> is not suitable match for std::array<int, 3> but class Template<T1,size> is suitable match for Template<int, 3>我不明白为什么std::array<T1, size>不适合匹配std::array<int, 3>但 class Template<T1,size>适合匹配Template<int, 3>

Because the 2nd template parameter's type of std::array is std::size_t , but not int ;因为第二个模板参数的std::array类型是std::size_t ,但不是int they don't match and cause template argument deduction on the non-type template parameter fails.它们不匹配并导致对非类型模板参数的模板参数推导失败。

(emphasis mine) (强调我的)

If a non-type template parameter is used in the parameter list, and the corresponding template argument is deduced, the type of the deduced template argument ( as specified in its enclosing template parameter list, meaning references are preserved) must match the type of the non-type template parameter exactly , except that cv-qualifiers are dropped, and except where the template argument is deduced from an array bound—in that case any integral type is allowed, even bool though it would always become true:如果在形参列表中使用了非类型模板形参,并且推导了相应的模板实参,则推导的模板实参的类型(在其封闭的模板形参列表中指定,意味着保留引用)必须与非类型模板参数完全,除了 cv 限定符被删除,并且模板参数是从数组绑定中推导出来的——在这种情况下,任何整数类型都是允许的,即使 bool 总是会变为真:

Change the type to std::size_t .将类型更改为std::size_t

template<typename T1, std::size_t size>
void print(std::array<T1, size> &arr)
{
    for (auto ele : arr)
        std::cout << ele << " ";
}

template<std::size_t size>
void print(std::array<float, size> &arr)
{
    std::cout << "its float" << std::endl;
    for (auto ele : arr)
        std::cout << ele << " ";
}

LIVE居住

You were close.你很亲密。 Use size_t instead of int.使用 size_t 而不是 int。 This compiles:这编译:

#include <iostream>
#include <array>

template<typename T1, size_t size>
void print(std::array<T1, size> const &arr)
{
    for (auto ele : arr)
        std::cout << ele << " ";
}

template<size_t size>
void print(std::array<float, size> &arr)
{
    std::cout << "its float" << std::endl;
    for (auto ele : arr)
        std::cout << ele << " ";
}

int main()
{
    std::array<int, 3> arr{1,3,4};
    std::array<float, 2> ar2{1.3, 3.4};
    print(arr);
    print(ar2);
    return 0;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM