Opened 14 years ago

Closed 13 years ago

Last modified 13 years ago

#2748 closed Patches (wontfix)

[any] implement reset for direct constructing (adapt to noncopyable)

Reported by: nowake@… Owned by: nasonov
Milestone: Boost 1.39.0 Component: any
Version: Boost 1.38.0 Severity: Optimization
Keywords: any noncopyable Cc:

Description

I modified boost::any to adapt noncopyable object.

(a) add reset members to construct content directory. (b) modify boost::any::holder

Sample:

  struct A {
     A() : v() {};
     A(unsigned int value) : v(value) {};
     unsigned int v;
  };

  BOOST_AUTO_TEST_CASE(test01)
  {
     any test;
     test.reset<A>(10);
     BOOST_CHECK_EQUAL(any_cast<A>(test).v, 10);
     test.reset();
     BOOST_CHECK_EQUAL(test.empty(), true);
  }


  struct B : boost::noncopyable {
     B() : v1(), v2() {};
     B(unsigned int value1, unsigned int value2)
        : v1(value1) ,v2(value2)
     {};
     unsigned int v1, v2;
  };

  BOOST_AUTO_TEST_CASE(test02)
  {
     any test;
     //test = B();                 // compile error
     //test.reset<B>(10, 20);      // compile error
     test.reset<B, false>(10, 20);
     //any test2(test);            // throw
     any test2;
     //test2 = test;               // throw
     BOOST_CHECK_EQUAL(any_cast<B&>(test).v1, 10);
     BOOST_CHECK_EQUAL(any_cast<B&>(test).v2, 20);
  }

Attachments (1)

any.hpp (9.3 KB ) - added by nowake@… 14 years ago.
modified boost::any

Download all attachments as: .zip

Change History (3)

by nowake@…, 14 years ago

Attachment: any.hpp added

modified boost::any

comment:1 by anonymous, 13 years ago

Resolution: wontfix
Status: newclosed

I think that a better and simpler approach for storing noncopyable objects is any_cast transparency to shared pointers. For instance:

any p(shared_ptr<X>(new X));
any_cast<X&>(p); // doesn't throw

Alex

comment:2 by nasonov, 13 years ago

Anonymous who closed the bug was me.

Note: See TracTickets for help on using tickets.