模板类接受std :: vector <t>或std :: array <t>

时间:2017-06-23 11:26:59

标签: c++ c++11 templates sfinae c++17

我正在尝试编写一个模板化的getter函数,除了std::array<T>std::vector<T>以及T类型的任意内容并返回一个值

Map2d.h

#include <vector>
#include <array>

class Map2d {
    private:
    unsigned int m_width;
    unsigned int m_height;
    unsigned int m_size;

    public:
    Map2d(unsigned int width, unsigned int height)
        : m_width(width), m_height(height) {
        m_size = m_width * m_height;
    }

    template <typename T>
    struct is_array_or_vector {
        enum { value = false };
    };

    template <typename T, typename A>
    struct is_array_or_vector<std::vector<T, A>> {
        enum { value = true };
    };

    template <typename T, std::size_t N>
    struct is_array_or_vector<std::array<T, N>> {
        enum { value = true };
    };

    template <typename V, template <typename, typename...> class T, typename... Args>
    typename std::enable_if<is_array_or_vector<T<V, Args...>>::value, V>::type
    get(const T<V, Args...>& con, const unsigned int x, const unsigned int y) {
        assert(con.size() <= m_size);
        return con[m_width * y + x];
    }
};

Main.cpp的

#include "Map2d.h"
int main() {
    Map2d map(10, 10);

    std::vector<int> v(100);
    std::cout << map.get(v, 5, 5) << std::endl; // works

    std::array<int, 100> a;
    std::cout << map.get(a, 5, 5) << std::endl; // not working

    std::list<int> l(100);
    std::cout << map.get(l, 5, 5) << std::endl; // should not work
    return 1;
}

我需要更改才能使其正常工作?我的版本可与此answer进行比较,区别在于返回值无效且不灵活。

我很感激每一个提示! :)

3 个答案:

答案 0 :(得分:6)

template <typename T>
typename std::enable_if<is_array_or_vector<T>::value, typename T::value_type>::type
get(const T& con, const unsigned int x, const unsigned int y) {
    assert(con.size() <= m_size);
    return con[m_width * y + x];
}

答案 1 :(得分:1)

如果代替is_array_of_vector,则开发定义(或不​​定义)所包含类型的自定义类型特征,如下所示

template <typename>
struct enableAV
 { };

template <typename T, typename ... Ts>
struct enableAV<std::vector<T, Ts...>>
 { using type = T; };

template <typename T, std::size_t N>
struct enableAV<std::array<T, N>>
 { using type = T; };

您可以避免std::enable_if并简单地写下get()

template <typename C>
typename enableAV<C>::type
   get(C const & con, const unsigned int x, const unsigned int y)
 {
   assert(con.size() <= m_size);
   return con[m_width * y + x];
 }

答案 2 :(得分:1)

namespace notstd {
  namespace details {
    template<template<class...>class, class, class...>
    struct can_apply:std::false_type{};
    template<class...>struct voider{using type=void;};
    template<class...Ts>using void_t=typename voider<Ts...>::type;

    template<template<class...>class Z, class...Ts>
    struct can_apply<Z, void_t<Z<Ts...>>, Ts...>:std::true_type{};
  }
  template<template<class...>class Z, class...Ts>
  using can_apply = details::can_apply<Z,void,Ts...>;
}
template<class T>
using dot_size_r = decltype( std::declval<T>().size() );
template<class T, class I=std::size_t>
using index_r = decltype( std::declval<T>()[ std::declval<I>() ] );

template<class T>
using can_dot_size = notstd::can_apply< dot_size_r, T >;
template<class T, class I=std::size_t>
using can_index = notstd::can_apply< index_r, T, I >;

can_dot_sizecan_index会告诉您类型T是否可以.size() d和[]编入索引。

template <typename C>
typename std::enable_if<
  can_dot_size<C const&>{} && can_index<C const&, unsigned int>{},
  typename std::decay<index_r<C const&, unsigned int>>::type
>::type
get(C const & con, const unsigned int x, const unsigned int y)
{
  assert(con.size() <= m_size);
  return con[m_width * y + x];
}

get适用于任何具有.size()方法和[]索引的任何接受unsigned int的内容。

这包括std::arraystd::vectorstd::dequestd::string或其他一些假装为数组的类型。

如果我们想限制连续的容器(可能是您的意图),而不是原始的.data()[],我们可能需要[]