{"id":318235,"date":"2021-02-18T15:06:33","date_gmt":"2021-02-18T15:06:33","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=318235"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=318235","title":{"rendered":"CRTP: \u041f\u0440\u0438\u043c\u0435\u0440 \u043d\u0430 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0435 \u00ab\u041c\u043e\u0441\u0442\u00bb"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<h4>\u0414\u043b\u044f \u043a\u043e\u0433\u043e<\/h4>\n<p>\u042d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u0430\u043d\u0430 \u043d\u0430 \u0442\u0435\u0445, \u043a\u0442\u043e \u043d\u0435 \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0441\u044f \u0441 \u0438\u0434\u0438\u043e\u043c\u043e\u0439 CRTP (Curiously recurring template pattern), \u043d\u043e \u0438\u043c\u0435\u0435\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u0448\u0430\u0431\u043b\u043e\u043d\u044b \u0432 C++. \u0421\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0437\u043d\u0430\u043d\u0438\u0439 \u0438\u043b\u0438 \u0442\u0432\u0435\u0440\u0434\u043e\u0433\u043e \u0432\u043b\u0430\u0434\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043d\u0430 \u0448\u0430\u0431\u043b\u043e\u043d\u0430\u0445 \u0434\u043b\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u044c\u0438 \u0432\u0430\u043c \u043d\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f.<\/p>\n<p>\u041f\u0443\u0441\u0442\u044c \u0443 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u0442\u0430\u043a\u0430\u044f <strong>\u0437\u0430\u0434\u0430\u0447\u043a\u0430:<\/strong><\/p>\n<p>\u0418\u0437 \u0441\u0435\u0442\u0438 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0444\u0430\u0439\u043b \u0432 \u043e\u0434\u043d\u043e\u043c \u0438\u0437 \u0444\u043e\u0440\u043c\u0430\u0442\u043e\u0432: <strong>json<\/strong> \u0438\u043b\u0438 <strong>xml<\/strong> \u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0438\u0445 \u0440\u0430\u0441\u043f\u0430\u0440\u0441\u0438\u0442\u044c \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u0430\u043a\u0443\u044e-\u0442\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e. \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0441\u0430\u043c\u043e \u0441\u043e\u0431\u043e\u0439 &#8212; \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u0442\u0442\u0435\u0440\u043d <strong>\u043c\u043e\u0441\u0442<\/strong> \u0434\u043b\u044f \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u043f\u0430\u0440\u0441\u0435\u0440\u0430 \u0438 \u0434\u0432\u0443\u0445 \u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439, \u043f\u043e \u043e\u0434\u043d\u043e\u0439 \u043d\u0430 \u0444\u043e\u0440\u043c\u0430\u0442 \u0444\u0430\u0439\u043b\u0430. \u0422\u0430\u043a, \u043f\u043e\u0441\u043b\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0444\u043e\u0440\u043c\u0430\u0442\u0430 \u0444\u0430\u0439\u043b\u0430, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0430 \u043d\u0443\u0436\u043d\u0443\u044e \u043d\u0430\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0432 \u0432\u0438\u0434\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f.<\/p>\n<h4>\u0421\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440<\/h4>\n<pre><code class=\"cpp\">\/\/ \u0424\u0443\u043d\u043a\u0446\u0438\u044f, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 Parser \u0432 \u0432\u0438\u0434\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f \/\/ \u0438 \u0444\u0430\u0439\u043b, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0430\u044f \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u043e\u043c \u0432\u0438\u0434\u0435 ParsedDataType parseData(Parser* parser, FileType file);  int main() {     FileType file = readFile();     Parser* impl = nullptr;     if (file.type() == JsonFile)         impl = new ParserJsonImpl();     else         impl = new ParserXmlImpl();     ParsedDataType parsedData = parserData(impl, file); }<\/code><\/pre>\n<p>\u0412 \u0442\u0430\u043a\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0435 \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e <strong>\u043c\u0438\u043d\u0443\u0441\u043e\u0432<\/strong>:<\/p>\n<ul>\n<li>\n<p>\u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 Parser \u043e\u0431\u044f\u0437\u0430\u043d \u0438\u043c\u0435\u0442\u044c \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0430 \u043a\u0430\u043a \u043c\u044b \u0437\u043d\u0430\u0435\u043c, \u0445\u043e\u0434\u0438\u0442\u044c \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0434\u043e\u0440\u043e\u0433\u043e.<\/p>\n<\/li>\n<li>\n<p>\u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0435 \u0442\u0430\u043a \u043d\u0430\u0433\u043b\u044f\u0434\u0435\u043d, \u043a\u0430\u043a \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b, \u0435\u0441\u043b\u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u044f\u0437\u044b\u043a\u0430\u043c\u0438 \u0441 \u0431\u043e\u0433\u0430\u0442\u044b\u043c\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u0430\u043c\u0438 \u0442\u0438\u043f\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 (\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043d\u0443\u043b\u0435\u0432\u043e\u0433\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f, \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 \u043a\u0443\u0447\u0435 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441 \u044d\u0442\u0438\u043c \u043c\u0438\u043d\u0443\u0441\u044b).<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u043e\u0432 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438 \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u0432 C++<\/p>\n<p><strong>CRTP<\/strong> &#8212; \u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u0441\u043d\u0430\u0440\u0443\u0436\u0438, \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u043d\u0443\u0442\u0440\u0438 \u043f\u0430\u0442\u0442\u0435\u0440\u043d, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043c\u0435\u0445\u0430\u043d\u0438\u043a\u0443 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043d\u0430 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0438\u0436\u0435.<\/p>\n<p>\u0421\u043c\u044b\u0441\u043b \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0442\u043e\u0442 \u0436\u0435 &#8212; \u043c\u044b \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u043e\u0432-\u043f\u043e\u0442\u043e\u043c\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c, \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u0435\u043b\u0430\u0435\u043c \u044d\u0442\u043e \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0438\u043d\u0430\u0447\u0435, \u043d\u0430 \u0448\u0430\u0431\u043b\u043e\u043d\u0430\u0445, \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438.<\/p>\n<h4>\u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043f\u0430\u0440\u0441\u0435\u0440\u0430<\/h4>\n<pre><code class=\"cpp\">template &lt;typename Implementation&gt; struct ParserInterface {      ParsedData getData() {         return impl()-&gt;getDataImpl();     }      ParsedID getID() {         return impl()-&gt;getIDImpl();         }  private:     Implementation* impl() {         return static_cast&lt;Implementation*&gt;(this);     } };<\/code><\/pre>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u0431\u044f\u0437\u0430\u043d\u0430 \u0431\u044b\u0442\u044c \u0435\u0433\u043e \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u043e\u043c \u0434\u043b\u044f \u0443\u0434\u0430\u0447\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0442\u0438\u043f\u043e\u0432 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f \u043d\u0430 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043a \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044e \u043d\u0430 \u043f\u043e\u0442\u043e\u043c\u043a\u0430 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>Implementation* impl()<\/code>.<\/p>\n<p>\u0412 \u043a\u0430\u0436\u0434\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u043c\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043c\u0435\u0442\u043e\u0434 \u043a\u043b\u0430\u0441\u0441\u0430-\u043f\u043e\u0442\u043e\u043c\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u043e\u043b\u0436\u0435\u043d \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u0434\u043b\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0435\u0441\u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u0432 \u043a\u043b\u0430\u0441\u0441\u0435-\u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u0435, \u0442\u043e \u0443 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438.<\/p>\n<p>\u0410 \u0432\u043e\u0442 \u043a\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430\u0448\u0438\u0445 \u043f\u0430\u0440\u0441\u0435\u0440\u043e\u0432<\/p>\n<h4>\u0418\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/h4>\n<pre><code class=\"cpp\">struct ParserJsonImpl : public ParserInterface&lt;ParserJsonImpl&gt; {     friend class ParserInterface; private:     ParsedData getDataImpl() {         std::cout &lt;&lt; \"ParserJsonImpl::getData()\\n\";         return ParsedData();     }      ParsedID getIDImpl() {         std::cout &lt;&lt; \"ParserJsonImpl::getID()\\n\";         return ParsedID;         } };  struct ParserXmlImpl : public ParserInterface&lt;ParserXmlImpl&gt; {     friend class ParserInterface; private:     ParsedData getDataImpl() {         std::cout &lt;&lt; \"ParserXmlImpl::getData()\\n\";         return ParsedData();     }      ParsedID getIDImpl() {         std::cout &lt;&lt; \"ParserXmlImpl::getID()\\n\";         return ParsedID();         } };<\/code><\/pre>\n<p>\u041a\u0430\u0436\u0434\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u0442\u0441\u044f \u043e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430, \u043f\u0440\u0438\u0447\u0435\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u0443\u0435\u0442 \u0435\u0433\u043e \u0441\u043e\u0431\u043e\u0439. \u0424\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438, \u043a\u0430\u0436\u0434\u0430\u044f \u0438\u0437 \u043d\u0438\u0445 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0440\u0430\u0437\u043d\u044b\u0439 \u0442\u0438\u043f, \u0442\u0430\u043a \u043a\u0430\u043a <code>ParserInterface&lt;A&gt;<\/code>\u0438 <code>ParserInterface&lt;B&gt;<\/code>\u044d\u0442\u043e \u0440\u0430\u0437\u043d\u044b\u0435 \u0442\u0438\u043f\u044b. \u041e\u0434\u043d\u0430\u043a\u043e \u043d\u0430 \u044d\u0442\u043e\u043c \u043f\u043e\u043a\u0430 \u043d\u0435 \u0441\u0442\u043e\u0438\u0442 \u0437\u0430\u043e\u0441\u0442\u0440\u044f\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435. \u0421\u0443\u0442\u044c \u043b\u0438\u0448\u044c \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0432 \u0443\u0433\u043b\u043e\u0432\u044b\u0445 \u0441\u043a\u043e\u0431\u043a\u0430\u0445 \u043a\u0430\u043a \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0448\u0430\u0431\u043b\u043e\u043d\u0430 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 &#8212; \u044d\u0442\u043e \u0442\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0442\u0438\u043f \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e<strong> <\/strong><code>static_cast&lt;&gt;()<\/code> \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>Implementation* impl()<\/code>. \u0410 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u043e\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430, \u0438\u043d\u0430\u0447\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043d\u0435 \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u0418 \u0442\u0430\u043a\u0430\u044f \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0446\u0435\u043f\u043e\u0447\u043a\u0430 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043d\u0430\u0441 \u0438\u043c\u0435\u043d\u043d\u043e \u043a \u0442\u0430\u043a\u043e\u043c\u0443 \u043f\u043e\u0434\u0445\u043e\u0434\u0443.<\/p>\n<p><strong>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043d\u0430\u0448\u0438\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439:<\/strong><\/p>\n<ol>\n<li>\n<p>\u041d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u043e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430, \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u0443\u044f \u0435\u0433\u043e \u0441\u043e\u0431\u043e\u0439 &#8212; \u043e\u0431 \u044d\u0442\u043e\u043c \u0432\u044b\u0448\u0435.<\/p>\n<\/li>\n<li>\n<p>\u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0438-\u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0435 \u043f\u043e\u043f\u0430\u043b\u0438 \u0432 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u0438\u0445 \u0432 \u0431\u043b\u043e\u043a\u0435 private.<\/p>\n<\/li>\n<li>\n<p>\u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043a\u043b\u0430\u0441\u0441 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u0432\u0438\u0434\u0435\u043b \u0444\u0443\u043d\u043a\u0446\u0438\u0438-\u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u0435\u0433\u043e \u043a\u0430\u043a <strong>friend<\/strong>.<\/p>\n<\/li>\n<\/ol>\n<p>\u0418\u0442\u0430\u043a, \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u043f\u043e\u0447\u0442\u0438 \u0433\u043e\u0442\u043e\u0432, \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0435\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f.<\/p>\n<h4>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0430 \u0444\u0430\u0439\u043b\u0430<\/h4>\n<pre><code class=\"cpp\">template &lt;typename Impl&gt; std::pair&lt;ParsedData, parsedID&gt; parseFile(ParserInterface&lt;Impl&gt; parser) {     return std::make_pair(parser.getData(), parser.getID()); }<\/code><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u0447\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u0438\u043c\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044f. \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>ParserInterface parser<\/code> \u043f\u0440\u044f\u043c\u043e \u043f\u0440\u043e\u0441\u0438\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 \u043d\u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430. \u0422\u0430\u043a\u0436\u0435 \u043c\u044b \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u043d\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438, \u0442\u0430\u043a \u043a\u0430\u043a <code>static_cast<\/code> \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0432 \u043e\u0434\u043d\u043e\u0439 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438, \u043d\u0435 \u0438\u043c\u0435\u044e\u0449\u0438\u0445 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043b\u0435\u0433\u0430\u043b\u0435\u043d.<\/p>\n<h4>\u041f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0448 \u043a\u043e\u0434:<\/h4>\n<pre><code class=\"cpp\">int main() {      ParserJsonImpl jsonParser;     parseFile(jsonParser);      ParserXmlImpl xmlParser;     parseFile(xmlParser);            return 0; }<\/code><\/pre>\n<p>\u0412\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043a\u0430\u043a \u0438 \u043e\u0436\u0438\u0434\u0430\u043b\u043e\u0441\u044c.<\/p>\n<h4>\u0412\u044b\u0432\u043e\u0434 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b<\/h4>\n<pre><code class=\"cpp\">ParserJsonImpl::getData() ParserJsonImpl::getID() ParserXmlImpl::getData() ParserXmlImpl::getID() <\/code><\/pre>\n<p><strong>\u0418\u0442\u0430\u043a<\/strong>, \u043c\u044b \u0443\u0448\u043b\u0438 \u043e\u0442 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 \u0438 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 \u043a\u0443\u0447\u0435, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u043b\u0438 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043d\u0430\u0448\u0435\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b. \u041e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u0438\u043b\u0438 \u043a\u043e\u0434 \u0442\u0435\u043c, \u0447\u0442\u043e \u0434\u0430\u043b\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043b\u0435\u0433\u0430\u043b\u044c\u043d\u043e\u0433\u043e <code>static_cast<\/code> \u0434\u043b\u044f \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0442\u0438\u043f\u043e\u0432 \u0432 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438. \u0422\u0430\u043a\u0436\u0435 \u0441\u0443\u0431\u044a\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u043c\u044b \u043f\u0435\u0440\u0435\u0448\u043b\u0438 \u043a \u0431\u043e\u043b\u0435\u0435 \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041e\u0434\u043d\u0430\u043a\u043e, \u0432\u0435\u0437\u0434\u0435 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0438 <strong>\u043c\u0438\u043d\u0443\u0441\u044b:<\/strong><\/p>\n<ul>\n<li>\n<p>\u0414\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u0448\u0430\u0431\u043b\u043e\u043d\u044b. \u0414\u043b\u044f \u043c\u043d\u043e\u0433\u0438\u0445 \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u0441\u043b\u043e\u0436\u043d\u0435\u0435, \u0447\u0435\u043c \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0438 \u0434\u043e \u043f\u0440\u0438\u0432\u044b\u043a\u0430\u043d\u0438\u044f \u043a \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u043e\u043c\u0443 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0443 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0434\u0438\u0441\u043a\u043e\u043c\u0444\u043e\u0440\u0442, \u0437\u0430\u0442\u043c\u0435\u0432\u0430\u044e\u0449\u0438\u0439 \u0432\u0441\u0435 \u043f\u043b\u044e\u0441\u044b.<\/p>\n<\/li>\n<li>\n<p>\u0428\u0430\u0431\u043b\u043e\u043d\u044b \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0441\u0438\u043b\u044c\u043d\u043e \u0441\u043a\u043e\u0432\u044b\u0432\u0430\u044e\u0442 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435: \u0443\u0436\u0435 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0442\u0430\u043a \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0438\u0437 \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u0432, \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441, \u0442\u0430\u043a \u043a\u0430\u043a \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u043d\u0438 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044e\u0442 \u0440\u0430\u0437\u043d\u044b\u043c \u043a\u043b\u0430\u0441\u0441\u0430\u043c, \u0445\u043e\u0442\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0442\u0430\u043a\u0430\u044f \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u0435\u0441\u0442\u044c.<\/p>\n<\/li>\n<\/ul>\n<h4>\u042d\u043f\u0438\u043b\u043e\u0433<\/h4>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0442\u0430\u043a\u0436\u0435 \u0434\u043b\u044f \u0438\u0434\u0438\u043e\u043c\u044b MixIn \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 &#171;\u043f\u043e\u0434\u043c\u0435\u0448\u0438\u0432\u0430\u044e\u0442&#187; \u0441\u0432\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u0430\u043c \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u0430\u043c. \u041e\u0434\u0438\u043d \u0438\u0437 \u0442\u0430\u043a\u0438\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 &#8212; <code>std::enable_shared_from_this<\/code> &#8212; \u043f\u043e\u0434\u043c\u0435\u0448\u0438\u0432\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f <code>shared_ptr<\/code> \u043d\u0430 \u0441\u0435\u0431\u044f \u0441\u0430\u043c\u043e\u0433\u043e.<\/p>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f \u043e\u0437\u043d\u0430\u043aParserInterface jsonParser;     parseFile(jsonParser);<\/p>\n<h4>\u041f\u043e\u043b\u043d\u044b\u0439 \u043b\u0438\u0441\u0442\u0438\u043d\u0433 \u0440\u0430\u0431\u043e\u0447\u0435\u0433\u043e \u043a\u043e\u0434\u0430<\/h4>\n<pre><code class=\"cpp\">#include &lt;iostream&gt;   template &lt;typename Implementation&gt; struct ParserInterface {     int getData() {         return impl()-&gt;getDataImpl();     }      int getID() {         return impl()-&gt;getIDImpl();         }  private:     Implementation* impl() {         return static_cast&lt;Implementation*&gt;(this);     } };  struct ParserJsonImpl : public ParserInterface&lt;ParserJsonImpl&gt; {     friend class ParserInterface&lt;ParserJsonImpl&gt;; private:     int getDataImpl() {         std::cout &lt;&lt; \"ParserJsonImpl::getData()\\n\";         return 0;     }      int getIDImpl() {         std::cout &lt;&lt; \"ParserJsonImpl::getID()\\n\";         return 0;     } };  struct ParserXmlImpl : public ParserInterface&lt;ParserXmlImpl&gt; {      int getDataImpl() {         std::cout &lt;&lt; \"ParserXmlImpl::getData()\\n\";         return 0;     }      int getIDImpl() {         std::cout &lt;&lt; \"ParserXmlImpl::getID()\\n\";         return 0;         } };  template &lt;typename Impl&gt; std::pair&lt;int, int&gt; parseFile(ParserInterface&lt;Impl&gt; parser) {     auto result = std::make_pair(parser.getData(), parser.getID());     return result; }   int main() {      ParserJsonImpl jsonParser;     parseFile(jsonParser);      ParserXmlImpl xmlParser;     parseFile(xmlParser);      return 0; }<\/code><\/pre>\n<\/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=\"https:\/\/habr.com\/ru\/post\/543098\/\"> https:\/\/habr.com\/ru\/post\/543098\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<h4>\u0414\u043b\u044f \u043a\u043e\u0433\u043e<\/h4>\n<p>\u042d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u0430\u043d\u0430 \u043d\u0430 \u0442\u0435\u0445, \u043a\u0442\u043e \u043d\u0435 \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0441\u044f \u0441 \u0438\u0434\u0438\u043e\u043c\u043e\u0439 CRTP (Curiously recurring template pattern), \u043d\u043e \u0438\u043c\u0435\u0435\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u0448\u0430\u0431\u043b\u043e\u043d\u044b \u0432 C++. \u0421\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0437\u043d\u0430\u043d\u0438\u0439 \u0438\u043b\u0438 \u0442\u0432\u0435\u0440\u0434\u043e\u0433\u043e \u0432\u043b\u0430\u0434\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043d\u0430 \u0448\u0430\u0431\u043b\u043e\u043d\u0430\u0445 \u0434\u043b\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u044c\u0438 \u0432\u0430\u043c \u043d\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f.<\/p>\n<p>\u041f\u0443\u0441\u0442\u044c \u0443 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u0442\u0430\u043a\u0430\u044f <strong>\u0437\u0430\u0434\u0430\u0447\u043a\u0430:<\/strong><\/p>\n<p>\u0418\u0437 \u0441\u0435\u0442\u0438 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0444\u0430\u0439\u043b \u0432 \u043e\u0434\u043d\u043e\u043c \u0438\u0437 \u0444\u043e\u0440\u043c\u0430\u0442\u043e\u0432: <strong>json<\/strong> \u0438\u043b\u0438 <strong>xml<\/strong> \u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0438\u0445 \u0440\u0430\u0441\u043f\u0430\u0440\u0441\u0438\u0442\u044c \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u0430\u043a\u0443\u044e-\u0442\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e. \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0441\u0430\u043c\u043e \u0441\u043e\u0431\u043e\u0439 &#8212; \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u0442\u0442\u0435\u0440\u043d <strong>\u043c\u043e\u0441\u0442<\/strong> \u0434\u043b\u044f \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u043f\u0430\u0440\u0441\u0435\u0440\u0430 \u0438 \u0434\u0432\u0443\u0445 \u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439, \u043f\u043e \u043e\u0434\u043d\u043e\u0439 \u043d\u0430 \u0444\u043e\u0440\u043c\u0430\u0442 \u0444\u0430\u0439\u043b\u0430. \u0422\u0430\u043a, \u043f\u043e\u0441\u043b\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0444\u043e\u0440\u043c\u0430\u0442\u0430 \u0444\u0430\u0439\u043b\u0430, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0430 \u043d\u0443\u0436\u043d\u0443\u044e \u043d\u0430\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0432 \u0432\u0438\u0434\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f.<\/p>\n<h4>\u0421\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440<\/h4>\n<pre><code class=\"cpp\">\/\/ \u0424\u0443\u043d\u043a\u0446\u0438\u044f, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 Parser \u0432 \u0432\u0438\u0434\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f \/\/ \u0438 \u0444\u0430\u0439\u043b, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0430\u044f \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u043e\u043c \u0432\u0438\u0434\u0435 ParsedDataType parseData(Parser* parser, FileType file);  int main() {     FileType file = readFile();     Parser* impl = nullptr;     if (file.type() == JsonFile)         impl = new ParserJsonImpl();     else         impl = new ParserXmlImpl();     ParsedDataType parsedData = parserData(impl, file); }<\/code><\/pre>\n<p>\u0412 \u0442\u0430\u043a\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0435 \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e <strong>\u043c\u0438\u043d\u0443\u0441\u043e\u0432<\/strong>:<\/p>\n<ul>\n<li>\n<p>\u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 Parser \u043e\u0431\u044f\u0437\u0430\u043d \u0438\u043c\u0435\u0442\u044c \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0430 \u043a\u0430\u043a \u043c\u044b \u0437\u043d\u0430\u0435\u043c, \u0445\u043e\u0434\u0438\u0442\u044c \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0434\u043e\u0440\u043e\u0433\u043e.<\/p>\n<\/li>\n<li>\n<p>\u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0435 \u0442\u0430\u043a \u043d\u0430\u0433\u043b\u044f\u0434\u0435\u043d, \u043a\u0430\u043a \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b, \u0435\u0441\u043b\u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u044f\u0437\u044b\u043a\u0430\u043c\u0438 \u0441 \u0431\u043e\u0433\u0430\u0442\u044b\u043c\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u0430\u043c\u0438 \u0442\u0438\u043f\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 (\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043d\u0443\u043b\u0435\u0432\u043e\u0433\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f, \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 \u043a\u0443\u0447\u0435 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441 \u044d\u0442\u0438\u043c \u043c\u0438\u043d\u0443\u0441\u044b).<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u043e\u0432 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438 \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u0432 C++<\/p>\n<p><strong>CRTP<\/strong> &#8212; \u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u0441\u043d\u0430\u0440\u0443\u0436\u0438, \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u043d\u0443\u0442\u0440\u0438 \u043f\u0430\u0442\u0442\u0435\u0440\u043d, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043c\u0435\u0445\u0430\u043d\u0438\u043a\u0443 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043d\u0430 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0438\u0436\u0435.<\/p>\n<p>\u0421\u043c\u044b\u0441\u043b \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0442\u043e\u0442 \u0436\u0435 &#8212; \u043c\u044b \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u043e\u0432-\u043f\u043e\u0442\u043e\u043c\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c, \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u0435\u043b\u0430\u0435\u043c \u044d\u0442\u043e \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0438\u043d\u0430\u0447\u0435, \u043d\u0430 \u0448\u0430\u0431\u043b\u043e\u043d\u0430\u0445, \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438.<\/p>\n<h4>\u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043f\u0430\u0440\u0441\u0435\u0440\u0430<\/h4>\n<pre><code class=\"cpp\">template &lt;typename Implementation&gt; struct ParserInterface {      ParsedData getData() {         return impl()-&gt;getDataImpl();     }      ParsedID getID() {         return impl()-&gt;getIDImpl();         }  private:     Implementation* impl() {         return static_cast&lt;Implementation*&gt;(this);     } };<\/code><\/pre>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u0431\u044f\u0437\u0430\u043d\u0430 \u0431\u044b\u0442\u044c \u0435\u0433\u043e \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u043e\u043c \u0434\u043b\u044f \u0443\u0434\u0430\u0447\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0442\u0438\u043f\u043e\u0432 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f \u043d\u0430 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043a \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044e \u043d\u0430 \u043f\u043e\u0442\u043e\u043c\u043a\u0430 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>Implementation* impl()<\/code>.<\/p>\n<p>\u0412 \u043a\u0430\u0436\u0434\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u043c\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043c\u0435\u0442\u043e\u0434 \u043a\u043b\u0430\u0441\u0441\u0430-\u043f\u043e\u0442\u043e\u043c\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u043e\u043b\u0436\u0435\u043d \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u0434\u043b\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0435\u0441\u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u0432 \u043a\u043b\u0430\u0441\u0441\u0435-\u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u0435, \u0442\u043e \u0443 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438.<\/p>\n<p>\u0410 \u0432\u043e\u0442 \u043a\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430\u0448\u0438\u0445 \u043f\u0430\u0440\u0441\u0435\u0440\u043e\u0432<\/p>\n<h4>\u0418\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/h4>\n<pre><code class=\"cpp\">struct ParserJsonImpl : public ParserInterface&lt;ParserJsonImpl&gt; {     friend class ParserInterface; private:     ParsedData getDataImpl() {         std::cout &lt;&lt; \"ParserJsonImpl::getData()\\n\";         return ParsedData();     }      ParsedID getIDImpl() {         std::cout &lt;&lt; \"ParserJsonImpl::getID()\\n\";         return ParsedID;         } };  struct ParserXmlImpl : public ParserInterface&lt;ParserXmlImpl&gt; {     friend class ParserInterface; private:     ParsedData getDataImpl() {         std::cout &lt;&lt; \"ParserXmlImpl::getData()\\n\";         return ParsedData();     }      ParsedID getIDImpl() {         std::cout &lt;&lt; \"ParserXmlImpl::getID()\\n\";         return ParsedID();         } };<\/code><\/pre>\n<p>\u041a\u0430\u0436\u0434\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u0442\u0441\u044f \u043e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430, \u043f\u0440\u0438\u0447\u0435\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u0443\u0435\u0442 \u0435\u0433\u043e \u0441\u043e\u0431\u043e\u0439. \u0424\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438, \u043a\u0430\u0436\u0434\u0430\u044f \u0438\u0437 \u043d\u0438\u0445 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0440\u0430\u0437\u043d\u044b\u0439 \u0442\u0438\u043f, \u0442\u0430\u043a \u043a\u0430\u043a <code>ParserInterface&lt;A&gt;<\/code>\u0438 <code>ParserInterface&lt;B&gt;<\/code>\u044d\u0442\u043e \u0440\u0430\u0437\u043d\u044b\u0435 \u0442\u0438\u043f\u044b. \u041e\u0434\u043d\u0430\u043a\u043e \u043d\u0430 \u044d\u0442\u043e\u043c \u043f\u043e\u043a\u0430 \u043d\u0435 \u0441\u0442\u043e\u0438\u0442 \u0437\u0430\u043e\u0441\u0442\u0440\u044f\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435. \u0421\u0443\u0442\u044c \u043b\u0438\u0448\u044c \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0432 \u0443\u0433\u043b\u043e\u0432\u044b\u0445 \u0441\u043a\u043e\u0431\u043a\u0430\u0445 \u043a\u0430\u043a \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0448\u0430\u0431\u043b\u043e\u043d\u0430 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 &#8212; \u044d\u0442\u043e \u0442\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0442\u0438\u043f \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e<strong> <\/strong><code>static_cast&lt;&gt;()<\/code> \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>Implementation* impl()<\/code>. \u0410 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u043e\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430, \u0438\u043d\u0430\u0447\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043d\u0435 \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u0418 \u0442\u0430\u043a\u0430\u044f \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0446\u0435\u043f\u043e\u0447\u043a\u0430 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043d\u0430\u0441 \u0438\u043c\u0435\u043d\u043d\u043e \u043a \u0442\u0430\u043a\u043e\u043c\u0443 \u043f\u043e\u0434\u0445\u043e\u0434\u0443.<\/p>\n<p><strong>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043d\u0430\u0448\u0438\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439:<\/strong><\/p>\n<ol>\n<li>\n<p>\u041d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u043e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430, \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u0443\u044f \u0435\u0433\u043e \u0441\u043e\u0431\u043e\u0439 &#8212; \u043e\u0431 \u044d\u0442\u043e\u043c \u0432\u044b\u0448\u0435.<\/p>\n<\/li>\n<li>\n<p>\u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0438-\u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0435 \u043f\u043e\u043f\u0430\u043b\u0438 \u0432 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u0438\u0445 \u0432 \u0431\u043b\u043e\u043a\u0435 private.<\/p>\n<\/li>\n<li>\n<p>\u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043a\u043b\u0430\u0441\u0441 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u0432\u0438\u0434\u0435\u043b \u0444\u0443\u043d\u043a\u0446\u0438\u0438-\u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u0435\u0433\u043e \u043a\u0430\u043a <strong>friend<\/strong>.<\/p>\n<\/li>\n<\/ol>\n<p>\u0418\u0442\u0430\u043a, \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u043f\u043e\u0447\u0442\u0438 \u0433\u043e\u0442\u043e\u0432, \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0435\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f.<\/p>\n<h4>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0430 \u0444\u0430\u0439\u043b\u0430<\/h4>\n<pre><code class=\"cpp\">template &lt;typename Impl&gt; std::pair&lt;ParsedData, parsedID&gt; parseFile(ParserInterface&lt;Impl&gt; parser) {     return std::make_pair(parser.getData(), parser.getID()); }<\/code><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u0447\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u0438\u043c\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044f. \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>ParserInterface parser<\/code> \u043f\u0440\u044f\u043c\u043e \u043f\u0440\u043e\u0441\u0438\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 \u043d\u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430. \u0422\u0430\u043a\u0436\u0435 \u043c\u044b \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u043d\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438, \u0442\u0430\u043a \u043a\u0430\u043a <code>static_cast<\/code> \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0432 \u043e\u0434\u043d\u043e\u0439 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438, \u043d\u0435 \u0438\u043c\u0435\u044e\u0449\u0438\u0445 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043b\u0435\u0433\u0430\u043b\u0435\u043d.<\/p>\n<h4>\u041f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0448 \u043a\u043e\u0434:<\/h4>\n<pre><code class=\"cpp\">int main() {      ParserJsonImpl jsonParser;     parseFile(jsonParser);      ParserXmlImpl xmlParser;     parseFile(xmlParser);            return 0; }<\/code><\/pre>\n<p>\u0412\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043a\u0430\u043a \u0438 \u043e\u0436\u0438\u0434\u0430\u043b\u043e\u0441\u044c.<\/p>\n<h4>\u0412\u044b\u0432\u043e\u0434 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b<\/h4>\n<pre><code class=\"cpp\">ParserJsonImpl::getData() ParserJsonImpl::getID() ParserXmlImpl::getData() ParserXmlImpl::getID() <\/code><\/pre>\n<p><strong>\u0418\u0442\u0430\u043a<\/strong>, \u043c\u044b \u0443\u0448\u043b\u0438 \u043e\u0442 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 \u0438 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 \u043a\u0443\u0447\u0435, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u043b\u0438 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043d\u0430\u0448\u0435\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b. \u041e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u0438\u043b\u0438 \u043a\u043e\u0434 \u0442\u0435\u043c, \u0447\u0442\u043e \u0434\u0430\u043b\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043b\u0435\u0433\u0430\u043b\u044c\u043d\u043e\u0433\u043e <code>static_cast<\/code> \u0434\u043b\u044f \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0442\u0438\u043f\u043e\u0432 \u0432 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438. \u0422\u0430\u043a\u0436\u0435 \u0441\u0443\u0431\u044a\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u043c\u044b \u043f\u0435\u0440\u0435\u0448\u043b\u0438 \u043a \u0431\u043e\u043b\u0435\u0435 \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041e\u0434\u043d\u0430\u043a\u043e, \u0432\u0435\u0437\u0434\u0435 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0438 <strong>\u043c\u0438\u043d\u0443\u0441\u044b:<\/strong><\/p>\n<ul>\n<li>\n<p>\u0414\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u0448\u0430\u0431\u043b\u043e\u043d\u044b. \u0414\u043b\u044f \u043c\u043d\u043e\u0433\u0438\u0445 \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u0441\u043b\u043e\u0436\u043d\u0435\u0435, \u0447\u0435\u043c \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0438 \u0434\u043e \u043f\u0440\u0438\u0432\u044b\u043a\u0430\u043d\u0438\u044f \u043a \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u043e\u043c\u0443 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0443 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0434\u0438\u0441\u043a\u043e\u043c\u0444\u043e\u0440\u0442, \u0437\u0430\u0442\u043c\u0435\u0432\u0430\u044e\u0449\u0438\u0439 \u0432\u0441\u0435 \u043f\u043b\u044e\u0441\u044b.<\/p>\n<\/li>\n<li>\n<p>\u0428\u0430\u0431\u043b\u043e\u043d\u044b \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0441\u0438\u043b\u044c\u043d\u043e \u0441\u043a\u043e\u0432\u044b\u0432\u0430\u044e\u0442 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435: \u0443\u0436\u0435 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0442\u0430\u043a \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0438\u0437 \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u0432, \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441, \u0442\u0430\u043a \u043a\u0430\u043a \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u043d\u0438 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044e\u0442 \u0440\u0430\u0437\u043d\u044b\u043c \u043a\u043b\u0430\u0441\u0441\u0430\u043c, \u0445\u043e\u0442\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0442\u0430\u043a\u0430\u044f \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u0435\u0441\u0442\u044c.<\/p>\n<\/li>\n<\/ul>\n<h4>\u042d\u043f\u0438\u043b\u043e\u0433<\/h4>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0442\u0430\u043a\u0436\u0435 \u0434\u043b\u044f \u0438\u0434\u0438\u043e\u043c\u044b MixIn \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 &#171;\u043f\u043e\u0434\u043c\u0435\u0448\u0438\u0432\u0430\u044e\u0442&#187; \u0441\u0432\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u0430\u043c \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u0430\u043c. \u041e\u0434\u0438\u043d \u0438\u0437 \u0442\u0430\u043a\u0438\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 &#8212; <code>std::enable_shared_from_this<\/code> &#8212; \u043f\u043e\u0434\u043c\u0435\u0448\u0438\u0432\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f <code>shared_ptr<\/code> \u043d\u0430 \u0441\u0435\u0431\u044f \u0441\u0430\u043c\u043e\u0433\u043e.<\/p>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f \u043e\u0437\u043d\u0430\u043aParserInterface jsonParser;     parseFile(jsonParser);<\/p>\n<h4>\u041f\u043e\u043b\u043d\u044b\u0439 \u043b\u0438\u0441\u0442\u0438\u043d\u0433 \u0440\u0430\u0431\u043e\u0447\u0435\u0433\u043e \u043a\u043e\u0434\u0430<\/h4>\n<pre><code class=\"cpp\">#include &lt;iostream&gt;   template &lt;typename Implementation&gt; struct ParserInterface {     int getData() {         return impl()-&gt;getDataImpl();     }      int getID() {         return impl()-&gt;getIDImpl();         }  private:     Implementation* impl() {         return static_cast&lt;Implementation*&gt;(this);     } };  struct ParserJsonImpl : public ParserInterface&lt;ParserJsonImpl&gt; {     friend class ParserInterface&lt;ParserJsonImpl&gt;; private:     int getDataImpl() {         std::cout &lt;&lt; \"ParserJsonImpl::getData()\\n\";         return 0;     }      int getIDImpl() {         std::cout &lt;&lt; \"ParserJsonImpl::getID()\\n\";         return 0;     } };  struct ParserXmlImpl : public ParserInterface&lt;ParserXmlImpl&gt; {      int getDataImpl() {         std::cout &lt;&lt; \"ParserXmlImpl::getData()\\n\";         return 0;     }      int getIDImpl() {         std::cout &lt;&lt; \"ParserXmlImpl::getID()\\n\";         return 0;         } };  template &lt;typename Impl&gt; std::pair&lt;int, int&gt; parseFile(ParserInterface&lt;Impl&gt; parser) {     auto result = std::make_pair(parser.getData(), parser.getID());     return result; }   int main() {      ParserJsonImpl jsonParser;     parseFile(jsonParser);      ParserXmlImpl xmlParser;     parseFile(xmlParser);      return 0; }<\/code><\/pre>\n<\/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=\"https:\/\/habr.com\/ru\/post\/543098\/\"> https:\/\/habr.com\/ru\/post\/543098\/<\/a><br \/><\/br><\/br><\/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-318235","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/318235","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=318235"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/318235\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=318235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=318235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=318235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}