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 612745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:55:35+00:00 2026-05-13T17:55:35+00:00

I’m trying to add a boost::ptr_vector to a std::deque, using push_back(). When I do,

  • 0

I’m trying to add a boost::ptr_vector to a std::deque, using push_back(). When I do, I get a BOOST::ASSERT for the typeid mismatch.

In “boost_ptr_container_clone_allocator”

     T* res = new T( r );
     BOOST_ASSERT( typeid(r) == typeid(*res) &&
                   "Default new_clone() sliced object!" );
     return res;

From TotalView, res and r:

Function "boost::new_clone<diagnostic_database_loader::DiagnosticDBClass>":
r:                                    (diagnostic_database_loader::DiagnosticDBClass  const &)
Local variables:
res:                                  0x082534f8 -> (diagnostic_database_loader::DiagnosticDBClass)

They look the same to me.

The ptr_vector I’m trying to add has instances of diagnostic_database_loader::JointDiagnosticDBClass, which is derived from the diagnostic_database_loader::DiagnosticDBClass above.

I printed out the typeid of the elements in the ptr_vector

boost::ptr_vector<DiagnosticDBClass> items(loader->getData());
>>> N26diagnostic_database_loader22JointDiagnosticDBClassE

I tried to reproduce this with a simple test program, but I’m not having the same problem.

#include "iostream"
#include <boost/ptr_container/ptr_vector.hpp>
#include <deque>

class Item
{
public:
  int my_val;
  Item() : my_val(1) { }

  int getMyVal() { return my_val; }
};
class SmallItem : public Item
{
public:
  SmallItem() : Item() { my_val = 2; }
};

class TinyItem : public SmallItem
{
public:
  TinyItem() : SmallItem() { my_val = 3; }
};

class MyClass
{
private:
  boost::ptr_vector<SmallItem> items_;  

public:
  MyClass() 
  { 
    for (int i = 0; i < 10; ++i)
    {
      SmallItem *it = new TinyItem();
      items_.push_back(it);
     }
  }

 std::auto_ptr<boost::ptr_vector<SmallItem> > getData() { return items_.release(); }
 };

std::deque<boost::ptr_vector<SmallItem> > Buffer;

int totalItems(boost::ptr_vector<SmallItem> &items)
{
  int total = 0;
  boost::ptr_vector<SmallItem>::iterator it;
  for (it = items.begin(); it != items.end(); ++it)
    total += (*it).getMyVal();
  return total;
}

int main(int argc, char **argv)
{
  MyClass cls;

  boost::ptr_vector<SmallItem> items(cls.getData());

  std::cout << "SmallItem typeid   " << typeid(items[0]).name() << std::endl;

  fprintf(stdout, "I found %d total!\n", totalItems(items));

  Buffer.push_back(items);

  fprintf(stdout, "I pushed back into the deque!\n");

   boost::ptr_vector<SmallItem> items2 = Buffer.front();
   Buffer.pop_front();
   fprintf(stdout, "I still found %d total in the new vector!\n", totalItems(items2));

  items2.release();
  fprintf(stdout, "I found %d total after I released!\n", totalItems(items2));

  return 0;
}

The test program works fine.

Does anyone know how to
* Reproduce the problem in the test code?
* Fix the problem in the real program?

If anyone wants the full code:
https://code.ros.org/svn/wg-ros-pkg/trunk/sandbox/diagnostic_database_loader

  • 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-05-13T17:55:35+00:00Added an answer on May 13, 2026 at 5:55 pm

    You should specialize the new_clone and delete_clone functions as described in the documentation.

    Alternatively you could specify your own clone allocator as the second argument of ptr_vector:

    class Item
    {
    public:
      int my_val;
      Item() : my_val(1) { }
    
      Item* clone() const
      {
        Item* item = do_clone();
        BOOST_ASSERT(typeid(*this) == typeid(*item) &&
                     "do_clone() sliced object!");
        return item;
      }
    
      int getMyVal() { return my_val; }
    
    private:
      // new virtual member function, overload in all derived classes.
      virtual Item* do_clone() const
      {
        return new Item(*this);
      }
    };
    
    class SmallItem : public Item
    {
    public:
      SmallItem() : Item() { my_val = 2; }
    
    private:
      virtual Item* do_clone() const
      {
        return new SmallItem(*this);
      }
    };
    
    struct ItemCloner
    {
      static Item* allocate_clone(const Item& item)
      {
        return item.clone();
      }
    
      static void deallocate_clone(const Item* item)
      {
        delete item;
      }
    };
    
    int main()
    {
       boost::ptr_vector<Item, ItemCloner> items;
       // and so on...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 371k
  • Answers 371k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should handle the WebBrowser.DocumentComplete event, once that event is… May 14, 2026 at 6:53 pm
  • Editorial Team
    Editorial Team added an answer ob_start(); $ini_sot = ini_get('short_open_tag'); ini_set('short_open_tag', 1); include('file_with_short_tags.php'); ini_set('short_open_tag', $ini_sot); $variable… May 14, 2026 at 6:53 pm
  • Editorial Team
    Editorial Team added an answer Tour Query should look like this SELECT tags.*, COUNT(*) AS… May 14, 2026 at 6:53 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.