push_back用于将临时容器添加到另一个容器

时间:2014-08-15 11:36:17

标签: c++ templates c++11 template-meta-programming

我正在编写一个push_back函数,用于将临时容器添加到另一个容器中。

它应该在使用push_back之前调整大小或保留容器(如果它们都可用,它应该更喜欢保留而不是调整大小)

目前的代码是:

namespace detail
{
    template<class>
    struct sfinae_true : std::true_type{};

    template<class T, class A0>
    static auto test_reserve(int)
        ->sfinae_true<decltype(std::declval<T>().reserve(std::declval<A0>()))>;
    template<class, class A0>
    static auto test_reserve(long)->std::false_type;

    template<class T, class A0>
    static auto test_resize(int)
        ->sfinae_true<decltype(std::declval<T>().resize(std::declval<A0>()))>;
    template<class, class A0>
    static auto test_resize(long)->std::false_type;
}

//check if class T have reserve(Arg) function
template<class T, class Arg>
struct HasReserve : decltype(detail::test_reserve<T, Arg>(0)){};


//check if class T have resize(Arg) function
template<class T, class Arg>
struct HasResize : decltype(detail::test_resize<T, Arg>(0)){};

//enable if resize function exists and there is no reserve function
template<class T1, class T2>
typename std::enable_if<HasResize< T1, typename T1::size_type>::value &&
    (!HasReserve< T1, typename T1::value_type>::value), void>::type
    inline reserveOrResize(T1& dst, T2&& src, typename T2::size_type newSize){
        static_assert(std::is_lvalue_reference<T1&>::value, "You must pass lvalue_refrence as first parameter");
        static_assert(std::is_rvalue_reference<T2&&>::value, "You must pass rvalue_refrence as second parameter");
        int loc = dst.size() + 1;
        dst.resize(newSize);
        std::cout << "Resizing\n";
        for (auto&& elem : src){
            dst[loc++] = std::move(elem);
        }
}

//enable if  reserve function exists
template<class T1, class T2>
typename std::enable_if<HasReserve< T1, typename T1::size_type>::value, void>::type
inline reserveOrResize(T1& dst, T2&& src, typename T2::size_type newSize){
    static_assert(std::is_lvalue_reference<T1&>::value, "You must pass lvalue_refrence as first parameter");
    static_assert(std::is_rvalue_reference<T2&&>::value, "You must pass rvalue_refrence as second parameter");
    dst.reserve(newSize);
    std::cout << "Reserving\n";
    for (auto&& elem : src){
        dst.push_back(std::move(elem));
    }
}

//push_back container T2 at the end of Container T1
//T2 must be rvalue refrence
template<class T1, class T2>
void push_back(T1& dst, T2&& src){
    static_assert(std::is_lvalue_reference<T1&>::value, "You must pass lvalue_refrence as first parameter");
    static_assert(std::is_rvalue_reference<T2&&>::value, "You must pass rvalue_refrence as second parameter");
    reserveOrResize(dst, std::move(src), src.size());
}

使用简单类型向量的容器 fine ,例如std::vector<int>

std::vector<int> vec;
push_back(vec,std::vector<int>(10));

live

但是当我使用它的类向量时它不会编译

struct A{};
int main(){   
    std::vector<A> vec;
    push_back(vec,std::vector<A>(10));//error
}

live

错误是:

main.cpp: In instantiation of 'void push_back(T1&, T2&&) [with T1 = std::vector<A> T2 = 
std::vector<A>]':
main.cpp:74:35:   required from here
main.cpp:67:49: error: call of overloaded 'reserveOrResize(std::vector<A>&, std::remove_reference<std::vector<A>&>::type, std::vector<A>::size_type)' is ambiguous
  reserveOrResize(dst, std::move(src), src.size());
                                                 ^
main.cpp:67:49: note: candidates are:
main.cpp:37:9: note: typename std::enable_if<(HasResize<T1, typename T1::size_type>::value && (! HasReserve<T1, typename T1::value_type>::value)), void>::type reserveOrResize(T1&, T2&&, typename T2::size_type) [with T1 = std::vector<A> T2 = std::vector<A> typename std::enable_if<(HasResize<T1, typename T1::size_type>::value && (! HasReserve<T1, typename T1::value_type>::value)), void>::type = void; typename T2::size_type = long unsigned int]
  inline reserveOrResize(T1& dst, T2&& src, typename T2::size_type newSize){
         ^
main.cpp:51:8: note: typename std::enable_if<HasReserve<T1, typename T1::size_type>::value, void>::type reserveOrResize(T1&, T2&&, typename T2::size_type) [with T1 = std::vector<A> T2 = std::vector<A> typename std::enable_if<HasReserve<T1, typename T1::size_type>::value, void>::type = void; typename T2::size_type = long unsigned int]
 inline reserveOrResize(T1& dst, T2&& src, typename T2::size_type newSize){
    ^

为什么它在第二个例子中失败了?!!!

1 个答案:

答案 0 :(得分:3)

错误就在这里

(!HasReserve< T1, typename T1::value_type>::value), void>::type

应该是typename T1::size_type,而不是value_type,因为您要检查value_type,该容器的方法保留参数类型T1::value_type({{1}在你的情况下),因为在向量中没有这样的方法,所以两个函数都可用。