#2748 closed Patches (wontfix)
[any] implement reset for direct constructing (adapt to noncopyable)
Reported by: | 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)
Change History (3)
by , 14 years ago
comment:1 by , 13 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
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
Note:
See TracTickets
for help on using tickets.
modified boost::any