简体   繁体   English

如何将 std::array 传递给可以接受 std::vector 的 function 模板

[英]How to pass a std::array to a function template which can accept std::vector

I'm trying to write a template, which can accept some sequence containers:我正在尝试编写一个模板,它可以接受一些序列容器:

template <typename S,
          typename = std::enable_if_t<
              std::is_same<S, std::array<typename S::value_type>, S::size()>::value ||
              std::is_same<S, std::vector<typename S::value_type>>::value>>
std::string arr2String(const S& seqContainer) {
    std::stringstream res;
    for (const auto& element : seqContainer) {
        res << element << "|";
    }
    return res.str();
}

However, this can't be compiled because of S::size() .但是,由于S::size()无法编译。 Obviously, there is no such a thing.显然,没有这样的事情。

Is it possible to make such a template function, which can handle std::vector and std::array ?是否可以制作这样一个模板 function,它可以处理std::vectorstd::array

If you want to restrict your template to work for only std::array or std::vector , you can write some helper traits如果您想限制您的模板仅适用于std::arraystd::vector ,您可以编写一些辅助特征

template <typename>
struct is_array : std::false_type {}

template <typename T, std::size_t N>
struct is_array<std::array<T, N>> : std::true_type {}

template <typename>
struct is_vector : std::false_type {}

template <typename... Ts>
struct is_vector<std::vector<Ts...>> : std::true_type {}

template <typename S,
          typename = std::enable_if_t<
              is_array<S>::value ||
              is_vector<S>::value>>
std::string arr2String(const S& seqContainer) {
    std::stringstream res;
    for (const auto& element : seqContainer) {
        res << element << "|";
    }
    return res.str();
}

Otherwise you can SFINAE on something more general否则你可以在更一般的东西上进行 SFINAE

template <typename S, typename = typename S::value_type>
std::string arr2String(const S& seqContainer) {
    std::stringstream res;
    for (const auto& element : seqContainer) {
        res << element << "|";
    }
    return res.str();
}

Your code could in fact be compiled if you instantiated an array and took its size, which is constexpr.如果您实例化一个数组并获取它的大小(即 constexpr),您的代码实际上可以被编译。

template <typename S,
        typename = std::enable_if_t<
            std::is_same<S, std::array<typename S::value_type, S{}.size()>>::value ||
            std::is_same<S, std::vector<typename S::value_type>>::value>>
std::string arr2String(const S& seqContainer) {
    std::stringstream res;
    for (const auto& element : seqContainer) {
        res << element << "|";
    }
    return res.str();
}

This of course would be problematic if default initialization of your value type is expensive.如果值类型的默认初始化很昂贵,这当然会出现问题。

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

相关问题 如何将模板类型(std::array 或 std::vector)传递给模板参数 - How to pass a template type (std::array or std::vector) to a template parameter 采用std :: vector或std :: array的模板函数 - Template function taking a std::vector or std::array 如何制作函数模板,它需要 std::vector 和指向函数的指针? - How to make function template, which takes std::vector and pointer to function? 如何通过引用相同的模板函数传递一行boost :: multi_array和std :: vector? - How to pass a row of boost::multi_array and std::vector by reference to the same template function? 如何通过std :: vector <void*> *作为std :: vector <MyStruct*> *功能 - How to pass std::vector<void*> * as std::vector<MyStruct*>* to a function 如何将动态数组(std :: vector)传递给odbc函数SQLBindParameter - How to pass a dynamic array ( std::vector ) to an odbc function SQLBindParameter 如何为std :: vector专门化模板成员函数<T> - How can I specialize a template member function for std::vector<T> 为什么我不能通过 std::vector<mytype> 到这个带有模板模板参数的 function ?</mytype> - Why can't I pass std::vector<MyType> to this function with template template parameters? std :: vector不接受我的struct作为模板 - std::vector doesnt accept my struct as template std::array 模板 function - std::array template function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM