Jump to content
  • Advertisement
Sign in to follow this  
too_many_stars

c++, passing std::container without template

This topic is 780 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Guys,

 

Quick question.

 

Is it possible to pass a std::container say std::vector, or std::list in a function paramater such as

 

void foo(some std::container p){

 for(auto it=p.begin();it!=p.end();it++){

 //do something with container

}

 

}

 

without having to use templates, ie

 

template <class container>

void foo(container &c){

 for(auto it=c.begin();it!=c.end();it++){

 //do something with container

 }

}

 

Thanks,

 

Mike

Share this post


Link to post
Share on other sites
Advertisement

They are not polymorphic, so no. You either have to template or just write every allowed overload.

 

Incidentally,

for(auto it=c.begin();it!=c.end();it++) {
  c->stuff;
}

can be

for(auto&& thing : c) {
  c.stuff;
}

Share this post


Link to post
Share on other sites

They are not polymorphic, so no. You either have to template or just write every allowed overload.
 
Incidentally,

for(auto it=c.begin();it!=c.end();it++) {
  c->stuff;
}
can be
for(auto&& thing : c) {
  c.stuff;
}



You almost certainly mean this:
for(auto it=c.begin();it!=c.end();it++) {
  it->stuff;
}
can be
for(auto&& thing : c) {
  thing.stuff;
}

Share this post


Link to post
Share on other sites

They are not polymorphic, so no.

 

For sure, they even are not (complete) types.

 

So if you know which container to use, one solution would be something like this:

 

template <class T>
void func(std::vector<T>& vec)
{
    vec.push_back(0);
}
 
void another_func()
{
   std::vector<int> vec1;
   func(vec1);
   std::vector<double> vec2;
   func(vec2);
}

Share this post


Link to post
Share on other sites
You could write a polymorphic wrapper if you _really_ want to... but you don't want to. Even the iterators would have to be polymorphic and hence _sloooow_.

Use a template. That's what they're for.

Though even better, use a template that operates on iterators (or ranges) and not containers. Overwhelming odds are you have zero reason to care about the container itself (especially if you are asking this question) and only need access to its contents. This is exactly why all the standard library algorithms operate on iterators rather than containers.

Share this post


Link to post
Share on other sites

 

They are not polymorphic, so no. You either have to template or just write every allowed overload.
 
Incidentally,

for(auto it=c.begin();it!=c.end();it++) {
  c->stuff;
}
can be
for(auto&& thing : c) {
  c.stuff;
}


You almost certainly mean this:
for(auto it=c.begin();it!=c.end();it++) {
  it->stuff;
}
can be
for(auto&& thing : c) {
  thing.stuff;
}

 

Yup. Thanks.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!