Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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(). Obviously, there is no such a thing.

Is it possible to make such a template function, which can handle std::vector and std::array?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you want to restrict your template to work for only std::array or std::vector, you can write some helper traits

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

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();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading