gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:44:26 25 4
gpt4 key购买 nike

我正在写一个 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());
}

很好适用于带有简单类型 vector 的容器,例如 std::vector<int>

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

live

但是当我将它与类 vector 一起使用时,它无法编译

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){
^

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

最佳答案

错误在这里

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

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

关于c++ - push_back 用于将临时容器添加到另一个容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25325739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com