型推論すごいねー
#include <boost/shared_ptr.hpp> #include <boost/make_shared.hpp> #include <iostream> struct Alloc { template<typename To> operator To() { return boost::make_shared<typename To::value_type>(); } }; struct A { A() { std::cout << "A::Ctor" << std::endl; } }; struct B { B() { std::cout << "B::Ctor" << std::endl; throw std::runtime_error("error!"); } }; int main() { boost::shared_ptr<A> a; a = Alloc(); boost::shared_ptr<B> b; try { b = Alloc(); } catch(std::exception& e) { std::cout << "catched : " << e.what() << std::endl; } }
ただ、これだと引数渡せない。