Thread: wrong new expression in [some_]allocator::construct

  1. #1
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168

    wrong new expression in [some_]allocator::construct

    So trying to make Bar non-copyable makes gcc barf. Example:

    Code:
    
    
    template<typename T>
    class Foo
    {
        private:
    
            struct Bar
            {
                T t_bar;
    
                Bar() {}
                Bar(T t) : t_bar(t) {}
    
                private:
    
                    Bar(const Bar&);
            };
    
            std::list<Bar> bars;
    
        public:
    
            Foo(T t)
            {
                Bar b(t);
                bars.push_back(b);
            }
    };
    
    int main()
    {
        Foo<int> foo(1);
    
        return 0;
    }
    So what, STL containers allocate some ghost object, then call the copy constructor of the object? Is there a way around this?
    goto( comeFrom() );

  2. #2
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Ok, before making a total ass of myself, am i trying to push_back a reference unintentionally?
    goto( comeFrom() );

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    A prerequisite for adding an element to a list (and pretty much all standard library containers) is that it be copyable.

    You're not storing a reference in the code, and you wouldn't want to because b goes out of scope when the constructor finishes. So if you're not storing a reference, then you must be storing a copy. If you make Bar non-copyable, then that isn't going to work.

    If you want to store objects in containers without copying them, you have to store references. Since references themselves aren't allowed to be stored in containers (yet), that means you must store a (preferably smart) pointer.

    You can also look into boost's ptr_container library for a ptr_list that handles most of the legwork for you so you don't have to manage the lifetimes of the pointers or memory yourself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expression: Convert infix notation to postfix notation.
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 02-27-2010, 07:44 AM
  2. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  3. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  4. Replies: 1
    Last Post: 12-01-2001, 08:17 AM
  5. Expression Evaluator
    By hei in forum C Programming
    Replies: 9
    Last Post: 11-13-2001, 10:50 AM