{"id":207294,"date":"2013-12-25T08:34:03","date_gmt":"2013-12-25T04:34:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=207294"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=207294","title":{"rendered":"<span class=\"post_title\">\u0418\u0434\u0438\u043e\u043c\u044b \u0421++. Type erasure<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t\u0425\u043e\u0442\u0438\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u0443\u0441\u0442\u0440\u043e\u0435\u043d <i>boost::function<\/i>, <i>boost::any<\/i> \u201c\u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c\u201d? \u0423\u0437\u043d\u0430\u0442\u044c \u0438\u043b\u0438 \u043e\u0441\u0432\u0435\u0436\u0438\u0442\u044c \u0432 \u043f\u0430\u043c\u044f\u0442\u0438, \u0447\u0442\u043e \u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0437\u0430 \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e\u0439 \u0444\u0440\u0430\u0437\u043e\u0439 \u201c\u0441\u0442\u0438\u0440\u0430\u043d\u0438\u0435 \u0442\u0438\u043f\u0430\u201d? \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u043a\u0440\u0430\u0442\u043a\u043e \u0438\u0437\u043b\u043e\u0436\u0438\u0442\u044c \u043c\u043e\u0442\u0438\u0432\u0430\u0446\u0438\u044e, \u0441\u0442\u043e\u044f\u0449\u0443\u044e \u0437\u0430 \u044d\u0442\u043e\u0439 \u0438\u0434\u0438\u043e\u043c\u043e\u0439 \u0438 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h4>\u041c\u043e\u0442\u0438\u0432\u0430\u0446\u0438\u044f<\/h4>\n<p>  \u041a\u0430\u043a \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u043e\u0434\u0438\u043d \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043d\u0438\u043a\u0430\u043a \u043d\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0434\u0440\u0443\u0433 \u0441 \u0434\u0440\u0443\u0433\u043e\u043c \u0442\u0438\u043f\u043e\u0432? \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u043e\u043f\u0446\u0438\u0438 \u0441\u0440\u0430\u0437\u0443 \u201c\u0440\u0430\u0437\u043b\u043e\u0436\u0438\u0442\u044c\u201d \u043f\u043e \u0440\u0430\u0437\u043d\u044b\u043c \u0442\u0438\u043f\u0430\u043c \u0438 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0435\u0434\u0438\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440. \u0418\u043b\u0438 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u201c\u043d\u0435\u0447\u0442\u043e\u201d \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0441 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435\u043c \u2014 \u043d\u0430\u043b\u0438\u0447\u0438\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u201c()\u201d \u0443 \u0445\u0440\u0430\u043d\u0438\u043c\u043e\u0433\u043e \u201c\u043d\u0435\u0447\u0442\u043e\u201d? \u041a\u0430\u043a, \u0432 \u043e\u0431\u0449\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u201c\u0441\u0442\u0435\u0440\u0435\u0442\u044c\u201d \u0442\u0438\u043f \u043b\u044e\u0431\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u0441\u043a\u0440\u044b\u0432 \u0435\u0433\u043e \u0437\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c \u0434\u0440\u0443\u0433\u043e\u0433\u043e, \u043d\u0435\u043a\u043e\u0435\u0433\u043e \u043e\u0431\u0449\u0435\u0433\u043e \u0442\u0438\u043f\u0430?<\/p>\n<h4>void*<\/h4>\n<p>  \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0432 \u0421++ \u0435\u0441\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u0441\u043a\u0440\u044b\u0442\u044c \u0442\u0438\u043f \u043b\u044e\u0431\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0437\u0430 \u043e\u0431\u0449\u0438\u043c \u0442\u0438\u043f\u043e\u043c. \u042d\u0442\u043e \u2014 \u0434\u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u0432 \u043d\u0430\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u043e \u043e\u0442 \u0421, \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c <i>void*<\/i>.<\/p>\n<p>  \u0415\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"cpp\">struct A{  void foo(); }; struct B{  int bar(double); }; A a; B b; std::vector&lt;void*&gt; v; v.push_back(&a); v.push_back(&b);  static_cast&lt;A*&gt;(v[0])-&gt;foo(); static_cast&lt;B*&gt;(v[1])-&gt;bar(3.5); <\/code><\/pre>\n<p>  \u0418\u043b\u0438 \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"cpp\">class void_any { public: \tvoid_any(const void* h, size_t size) : size_(size) \t{ \t\th_ = std::malloc(size); \t\tstd::memcpy(h_, h, size); \t} \tvoid get(void*& h) \t{ \t\th = std::malloc(size_); \t\tstd::memcpy(h, h_, size_); \t} \t~void_any(){ std::free(h_); } private: \tsize_t size_; \tvoid* h_; };  int some_int=675321; void_any va(&some_int, sizeof(int)); void* pi; va.get(pi); std::cout &lt;&lt; *(int*)pi &lt;&lt; std::endl; <\/code><\/pre>\n<p>  \u0422\u0430\u043a\u0430\u044f \u0441\u0445\u0435\u043c\u0430 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c, \u043d\u043e, \u0434\u0443\u043c\u0430\u044e, \u0435\u0451 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0438 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b. \u041c\u043e\u0436\u043d\u043e \u043e\u0448\u0438\u0431\u0438\u0442\u044c\u0441\u044f \u043f\u0440\u0438 \u043a\u0430\u0441\u0442\u0435, \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043d\u0435\u0432\u0435\u0440\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u043d\u0435\u043b\u044c\u0437\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441 <i>rvalue<\/i> \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u043c\u0438. \u041c\u044b \u0437\u0430\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043e \u0442\u043e\u043c, \u043e\u0431\u044a\u0435\u043a\u0442 \u043a\u0430\u043a\u043e\u0433\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0438\u043f\u0430 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435 \u0438 \u201c\u0432\u0440\u0443\u0447\u043d\u0443\u044e\u201d \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043a \u044d\u0442\u043e\u043c\u0443 \u0442\u0438\u043f\u0443. \u041d\u0443 \u0430 \u0441\u0430\u043c\u043e\u0435 \u0433\u043b\u0430\u0432\u043d\u044b\u0439 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a, \u043f\u043e\u0436\u0430\u043b\u0443\u0439, \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043c\u044b \u043d\u0438\u043a\u0430\u043a \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0442\u0438\u043f\u043e\u0432 \u044f\u0437\u044b\u043a\u0430 \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043f\u0438\u0448\u0435\u043c. \u0412\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u0447\u0442\u043e \u0437\u0430\u0431\u0438\u0432\u0430\u0442\u044c \u0433\u0432\u043e\u0437\u0434\u044c \u0448\u0443\u0440\u0443\u043f\u043e\u0432\u0435\u0440\u0442\u043e\u043c. \u041c\u043e\u0436\u043d\u043e, \u043d\u043e \u043d\u0435\u0443\u0434\u043e\u0431\u043d\u043e. \u0422\u0430\u043a \u043a\u0430\u043a \u0436\u0435 \u0431\u044b\u0442\u044c?<\/p>\n<h4>\u0428\u0430\u0431\u043b\u043e\u043d\u044b \u0438 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435<\/h4>\n<p>  \u0412\u044b \u0443\u0436\u0435 \u043d\u0430\u0432\u0435\u0440\u043d\u043e\u0435 \u0434\u043e\u0433\u0430\u0434\u0430\u043b\u0438\u0441\u044c, \u0447\u0442\u043e \u0431\u0435\u0437 \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u0432 \u0437\u0434\u0435\u0441\u044c \u043d\u0435 \u043e\u0431\u043e\u0439\u0434\u0435\u0442\u0441\u044f. \u0414\u0430, \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 (\u0448\u0430\u0431\u043b\u043e\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e) \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 \u043b\u044e\u0431\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0438, \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c, \u0441\u043a\u0440\u044b\u0442\u044c \u0435\u0433\u043e \u0442\u0438\u043f, \u043d\u043e \u044d\u0442\u0438\u043c \u043c\u044b \u043d\u0435 \u0440\u0435\u0448\u0438\u043c \u0432\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e, \u0441\u043a\u0440\u044b\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 \u043b\u044e\u0431\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0437\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c <b>\u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u0449\u0435\u0433\u043e<\/b> \u0442\u0438\u043f\u0430.   <\/p>\n<pre><code class=\"cpp\">template &lt;typename T&gt; struct some_t{}; some_t&lt;int&gt; s1; some_t&lt;double&gt; s2; <\/code><\/pre>\n<p>  \u0412\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0435 \u0432\u044b\u0448\u0435 <i>s1<\/i> \u0438 <i>s2<\/i> \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0441\u0442\u0430\u043d\u0446\u0438\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u0440\u0430\u0437\u043d\u044b\u0445, \u043d\u0435\u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432.<br \/>  \u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, \u0421++ \u043d\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043e\u0434\u043d\u0438\u043c\u0438 \u0448\u0430\u0431\u043b\u043e\u043d\u0430\u043c\u0438. \u0418 \u043d\u0430\u043c \u043d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c \u043f\u0440\u0438\u0434\u0435\u0442 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u043e\u043b\u0438\u043c\u043e\u0440\u0444\u0438\u0437\u043c. \u0427\u0438\u0442\u0430\u0439\u0442\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0430\u0437\u0434\u0435\u043b, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043d\u044f\u0442\u044c \u043a\u0430\u043a \u0438\u043c\u0435\u043d\u043d\u043e.<\/p>\n<h4>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h4>\n<p>  \u0418\u0442\u0430\u043a, \u043e\u0442 \u0441\u043b\u043e\u0432 \u043a \u0434\u0435\u043b\u0443. \u041d\u0430\u043c \u0443\u0436\u0435 \u044f\u0441\u043d\u043e, \u0447\u0442\u043e \u043d\u0430\u0448\u0430 \u201c\u043e\u0431\u0435\u0440\u0442\u043a\u0430\u201d \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u043c, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u0430 \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 \u043f\u0440\u0438\u043d\u044f\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 \u043b\u044e\u0431\u043e\u0433\u043e \u0442\u0438\u043f\u0430. \u041a\u0430\u043a \u044d\u0442\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e? \u041f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430.  <\/p>\n<pre><code class=\"cpp\">class any { public: \ttemplate&lt;typename T&gt; \tany(const T& t); \/\/\u2026 }; <\/code><\/pre>\n<p>  \u041d\u043e \u043a\u0430\u043a \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0442\u043e, \u0447\u0442\u043e \u043d\u0430\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u043b\u0438 \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435? \u041d\u0430\u0448 \u043a\u043b\u0430\u0441\u0441 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0437\u043d\u0430\u0435\u0442 \u043e \u0442\u0438\u043f\u0435 \u0422, \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u0443\u044e\u0449\u0435\u043c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0442\u0430\u043a \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c:  <\/p>\n<pre><code class=\"cpp\">class any { \/\/... private: \tT t_; }; <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u0443\u044e \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443, \u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0435 \u043d\u0430\u043c \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 <i>t<\/i>, \u043e\u0442\u0434\u0430\u0434\u0438\u043c \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443-\u0448\u0430\u0431\u043b\u043e\u043d, \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u043e\u0442 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u043e\u0439 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0431\u0430\u0437\u044b.<\/p>\n<pre><code class=\"cpp\">class any { public:  any(const T& t) : held_(new holder&lt;T&gt;(t)){} \/\/\u2026 private: \tstruct base_holder \t{ \t\tvirtual ~base_holder(){} \t}; \t \ttemplate&lt;typename T&gt; struct holder : base_holder \t{ \t\tholder(const T& t) : t_(t){} \t\tT t_; \t}; private: \tbase_holder* held_; }; <\/code><\/pre>\n<p>  \u041e\u0442\u043b\u0438\u0447\u043d\u043e! \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 \u043b\u044e\u0431\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 <i>\u201cany\u201d<\/i>. \u0414\u0435\u043b\u043e \u0437\u0430 \u043c\u0430\u043b\u044b\u043c, \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u043d\u0430\u0434\u043e \u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043a\u0430\u043a\u0438\u043c-\u0442\u043e \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u201c\u0434\u043e\u0441\u0442\u0430\u0442\u044c\u201d \u0438\u0437 \u043d\u0435\u0434\u0440 \u043d\u0430\u0448\u0435\u0439 \u043e\u0431\u0435\u0440\u0442\u043a\u0438. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e, \u043a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u043d\u0430\u043c \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f <i>RTTI<\/i>. \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0442\u0438\u043f\u0435 \u0445\u0440\u0430\u043d\u0438\u043c\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u043d\u0430\u0448\u0438 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b.<\/p>\n<pre><code class=\"cpp\">struct base_holder {\t\/\/... \tvirtual const std::type_info& type_info() const = 0; };  template&lt;typename T&gt; struct holder : base_holder {\t\/\/... \tconst std::type_info& type_info() const \t{ \t\treturn typeid(t_); \t} }; <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043d\u0435 \u0441\u043e\u0441\u0442\u0430\u0432\u0438\u0442 \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u0442\u0440\u0443\u0434\u0430.<\/p>\n<pre><code class=\"cpp\">template&lt;typename U&gt; U cast() const { \tif(typeid(U) != held_-&gt;type_info()) \t\tthrow std::runtime_error(&quot;Bad any cast&quot;); \treturn static_cast&lt;holder&lt;U&gt;* &gt;(held_)-&gt;t_; } <\/code><\/pre>\n<p>  \u041f\u043e\u0447\u0435\u043c\u0443 <i>RTTI<\/i> \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e? \u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e, \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435 \u0442\u0430\u043a\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0442\u0438\u043f\u0430 \u0432 compile time:  <\/p>\n<pre><code class=\"cpp\">U cast(typename std::enable_if&lt;std::is_same&lt;U, decltype(         static_cast&lt;holder&lt;U&gt;* &gt;(held_)-&gt;t_)&gt;::value&gt;::type* = 0) const \t{ \t\treturn static_cast&lt;holder&lt;U&gt;* &gt;(held_)-&gt;t_; \t} <\/code><\/pre>\n<p>  \u041f\u043e\u0447\u0435\u043c\u0443 \u0442\u0430\u043a\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442? \u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e   <\/p>\n<pre><code class=\"cpp\">std::is_same&lt;U, decltype(static_cast&lt;holder&lt;U&gt;* &gt;(held_)-&gt;t_)&gt;::value <\/code><\/pre>\n<p>  \u0432\u0441\u0435\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 <i>true<\/i>, \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u043e\u0442 \u0442\u043e\u0433\u043e \u043a\u0430\u043a\u043e\u0439 \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0442\u0438\u043f \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u0445\u0440\u0430\u043d\u044f\u0449\u0435\u0433\u043e\u0441\u044f \u0432 <i>holder<\/i>. \u0422\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0438 \u0434\u0430\u0436\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0431\u0435\u0437 \u043f\u0430\u0434\u0435\u043d\u0438\u0439 (\u0435\u0441\u043b\u0438 \u043f\u043e\u0432\u0435\u0437\u0435\u0442)  <\/p>\n<pre><code class=\"cpp\">any a(2); a.cast&lt;std::string&gt;(); <\/code><\/pre>\n<p>  \u041d\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0431\u0443\u0434\u0443\u0442 \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u0442\u0435, \u0447\u0442\u043e \u043e\u0436\u0438\u0434\u0430\u0435\u0442 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442.<\/p>\n<p>  \u0412 \u043a\u043b\u0430\u0441\u0441\u0435 <i>boost::function<\/i> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0442\u043e\u0442 \u0436\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u0441\u0442\u0438\u0440\u0430\u043d\u0438\u044f \u0442\u0438\u043f\u0430. \u041a\u043e\u0441\u043c\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043e\u0442\u043b\u0438\u0447\u0438\u044f \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u044e\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e <i>function<\/i> \u2014 \u0448\u0430\u0431\u043b\u043e\u043d, \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u0443\u0435\u043c\u044b\u0439 \u0442\u0438\u043f\u0430\u043c\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u0430 \u0432\u043e \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u0445 \u043f\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f  <\/p>\n<pre><code class=\"cpp\">virtual return_type operator()(arg_type1, .., arg_typeN); <\/code><\/pre>\n<h4>\u041b\u0438\u0441\u0442\u0438\u043d\u0433<\/h4>\n<pre><code class=\"cpp\">class any { public: \ttemplate&lt;typename T&gt; \tany(const T& t) : held_(new holder&lt;T&gt;(t)){} \t~any(){ delete held_; } \ttemplate&lt;typename U&gt; \tU cast() const \t{ \t\tif(typeid(U) != held_-&gt;type_info()) \t\t\tthrow std::runtime_error(&quot;Bad any cast&quot;); \t\treturn static_cast&lt;holder&lt;U&gt;* &gt;(held_)-&gt;t_; \t} private: \tstruct base_holder \t{ \t\tvirtual ~base_holder(){} \t\tvirtual const std::type_info& type_info() const = 0; \t}; \t \ttemplate&lt;typename T&gt; struct holder : base_holder \t{ \t\tholder(const T& t) : t_(t){} \t\tconst std::type_info& type_info() const \t\t{ \t\t\treturn typeid(t_); \t\t} \t\tT t_; \t}; private: \tbase_holder* held_; };  int main()  { \tany a(2); \tstd::cout &lt;&lt; a.cast&lt;int&gt;() &lt;&lt; std::endl; \tany b(std::string(&quot;abcd&quot;)); \ttry \t{ \t\tstd::cout &lt;&lt; b.cast&lt;double&gt;() &lt;&lt; std::endl; \t} \tcatch(const std::exception& e) \t{ \t\tstd::cout &lt;&lt; e.what() &lt;&lt; std::endl; \t} \treturn 0; } <\/code><\/pre>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/207294\/\"> http:\/\/habrahabr.ru\/post\/207294\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t\u0425\u043e\u0442\u0438\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u0443\u0441\u0442\u0440\u043e\u0435\u043d <i>boost::function<\/i>, <i>boost::any<\/i> \u201c\u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c\u201d? \u0423\u0437\u043d\u0430\u0442\u044c \u0438\u043b\u0438 \u043e\u0441\u0432\u0435\u0436\u0438\u0442\u044c \u0432 \u043f\u0430\u043c\u044f\u0442\u0438, \u0447\u0442\u043e \u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0437\u0430 \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e\u0439 \u0444\u0440\u0430\u0437\u043e\u0439 \u201c\u0441\u0442\u0438\u0440\u0430\u043d\u0438\u0435 \u0442\u0438\u043f\u0430\u201d? \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u043a\u0440\u0430\u0442\u043a\u043e \u0438\u0437\u043b\u043e\u0436\u0438\u0442\u044c \u043c\u043e\u0442\u0438\u0432\u0430\u0446\u0438\u044e, \u0441\u0442\u043e\u044f\u0449\u0443\u044e \u0437\u0430 \u044d\u0442\u043e\u0439 \u0438\u0434\u0438\u043e\u043c\u043e\u0439 \u0438 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-207294","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/207294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=207294"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/207294\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=207294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=207294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=207294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}