Suppose I have a template function that accepts different types of vectors (but for various reasons, I cannot mention this in the template parameter). Here's what I'm trying to do: insert a new, default-built element in a specific location, not knowing its type:
template <typename T> void foo(T* v) { v->insert(v->begin() + 5, decltype(v->at(0))()); }
This does not work, but gives you an idea of what I'm trying to do. I also tried using value_typeout std::vector, but I ran into problems there too . Any ideas how to solve this problem?
value_type
std::vector
Uncheck all "type name":
v->emplace(v->begin() + 5);
or
v->insert(v->begin() + 5, {});
, decltype(v->at(0)) . value_type , , , , , .
decltype(v->at(0))
, v std::vector - , , T , :
v
T
template <typename T> void foo(std::vector<T>* v) { v->insert(v->begin() + 5, T()); }
, v->insert() v->begin() + 5 . , v, , v->insert() v->begin(), begin() , + 5.
v->insert()
v->begin() + 5
v->begin()
begin()
+ 5
Source: https://habr.com/ru/post/1677939/More articles:Как передать совпадение при использовании рендеринга в компоненте Route из реагирующего маршрутизатора (v4) - reactjscustom color gradient map in Julia - julia-langWhy are the artists not in Concurrency TS and in the std :: future interface? - c ++Why not the parts of Concurrency TS that go in C ++ 17? - c ++Serving files and routes from web API - c #https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1677940/can-i-configure-ecs-to-automatically-purge-old-container-images-from-my-ec2-instances&usg=ALkJrhgRD89GQR2wAx5uruNEysy1ahGC5QAws API Gateway SDK Generation for React Native - react-nativePassing a listener object as a function parameter to kotlin - kotlinHow to find cluster and CIDR service of Kubernetes cluster? - kubernetesDjango admin unregister Sites - pythonAll Articles