Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8955065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:27:34+00:00 2026-06-15T14:27:34+00:00

I have a function that takes, as an argument, an stl vector of pointers

  • 0

I have a function that takes, as an argument, an stl vector of pointers to Foo.

However, I also have objects which are shared pointers to the same class Foo. I would like to be able to call this function feeding this other objects too. Do I have to overload the function? I seem to be unable to dynamic-cast it from vector<shared_ptr<Foo>> to vector<Foo*>.

I know one can convert a shared_ptr to a pointer with the Get() method, but what about this? Any ideas?

Update on the question:

I have implemented the solution suggested below, but now, when declaring all the possible types for the template function, I get:

“explicit instantiation of ‘void my_func(std::vector&, std::vector&) [with Ptr_Jet1 = Jet*, Ptr_Jet2 = Jet*]’ but no definition available”

and the same thing for all the other combinations (for example, Ptr_Jet1 being shared_ptr instead of Jet*.

On the .cpp file, I have:

template<typename Ptr_Jet1, typename Ptr_Jet2>
void my_func(vector<Ptr_Jet1> vec1, vector<Ptr_Jet2> vec2){
//definition
}

and on the .h file, I have:

typedef boost::shared_ptr<Jet> ptr_jet;

template<typename Ptr_Jet1, typename Ptr_Jet2>
void my_func(vector<Ptr_Jet1> vec1, vector<Ptr_Jet2> vec2);
//
template void my_func(vector<Jet*> vec1, vector<Jet*> vec2);
template void my_func(vector<Jet*> vec1, vector<ptr_jet> vec2);
template void my_func(vector<ptr_jet> vec1, vector<Jet*> vec2);
template void my_func(vector<ptr_jet> vec1, vector<ptr_jet> vec2);

I don’t get what’s wrong here…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:27:35+00:00Added an answer on June 15, 2026 at 2:27 pm

    If you wrote that function, I would suggest templatization, as shared_ptr has operator* just as you would use the raw pointers.

    template<typename _Tp_ptr_Foo>
    void your_fun(const vector<_Tp_ptr_Foo>& ); // or something alike here
    

    In this situation, templatization does essentially the same as function overloading, but with templatization you avoid duplicating your code.

    If you don’t have control over the function, you need to convert the whole vector. If it is not more than a 1000 elements and you don’t do it more than a couple hundred times, there’s not much performance loss to fear about.

    Unfortunately you cannot dynamic cast the one to the other as they are not related through inheritance. Although they look alike, vector<T> has nothing to do with vector<U>.

    UPDATE:
    I agree with Grizzly, template arguments are automatically deduced so you don’t need to explicitly write that out. So you can keep calling it your_fun(v).

    The only thing you need to be careful with: if you work with header and code files separately, you need to explicitly instruct the compiler that it should create both of your functions, like this:

    //header file:
    
    template<typename _Tp_ptr_Foo>
    void your_fun(const vector<_Tp_ptr_Foo>& ); 
    
    template void your_fun(const vector<Foo*>& ); 
    template void your_fun(const vector<shared_ptr<Foo> >& ); 
    
    //code file:
    
    template<typename _Tp_ptr_Foo>
    void your_fun(const vector<_Tp_ptr_Foo>& )
    {
      // implementation
    }
    

    UPDATE2: (answering Elelias’es comment)

    Your template declaration should look like this:

    // header file:
    
    template<typename _Tp1, typename _Tp2, typename _Tp3>
    void your_fun(const vector<_Tp1>&, const vector<_Tp2>&, const vector<_Tp3>& ); 
    

    After that, you have 2 options:

    1. You can put the definition into a separate code file. In that case, you’ll need 6 explicit template instantiations in the implementation file for the header, one for each combination.

    2. You can put the definition into the header and then you don’t need the 6 explicit instantiations. I would rather suggest this in this case. Although it does not separate declaration and implementation, it is not that bad solution. I have seen this approach in serious c++ libraries too, as an example you can have a look at operations.hpp in OpenCV.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function foo that takes a NxM numpy array as an argument
I have a function that takes a string-like argument. I want to decide if
Background I have a function that takes a config object as an argument. Within
I have a dll with a function that takes PyObject as argument something like
I have a function that takes a callback argument. This can be either a
I have a function that takes a pointer as a reference argument, but I
I have a C++ function that takes one std::string as an argument and a
I have a function that takes one argument: function magic(el){ ... } I have
I have a Python function that takes a numeric argument that must be an
I have a C function that takes a function pointer as argument, it's a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.