{"id":4374,"date":"2014-03-30T10:15:33","date_gmt":"2014-03-30T10:15:33","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/can-templates-be-completely-avoided-in-c-collection-of-common-programming-errors\/"},"modified":"2014-03-30T10:15:33","modified_gmt":"2014-03-30T10:15:33","slug":"can-templates-be-completely-avoided-in-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/can-templates-be-completely-avoided-in-c-collection-of-common-programming-errors\/","title":{"rendered":"Can templates be completely avoided in C++?-Collection of common programming errors"},"content":{"rendered":"<p>My question, which is in the last paragraph, needs (in my opinion) some explanatory setup. Basically, I&#8217;m wondering if you can avoid using templates if instead you make all your would-be template classes instead inherit from a base class that declares virtual methods you will be using, among them a function for memory allocation that, when implemented, will return a pointer to the derived (not base) type.<\/p>\n<h2>BEGIN setup<\/h2>\n<p>C++ does not seem to have the notion of a &#8220;universal base class&#8221; from which everything is implicitly derived; I imagine that class would be defined like this:<\/p>\n<pre><code>class universal_base\n{\n};\n<\/code><\/pre>\n<p>Of course, now that I have defined it, I can make all my classes derive from it. Then, because of polymorphism, any references or pointers to <code>universal_base<\/code> that I pass around will be basically the same as template parameters:<\/p>\n<pre><code>template \n\nclass C\n{\n   T &amp;x;\n   int f(T &amp;y);\n   C(T &amp;z): x(z + 1) {}\n};\n\nclass C\n{\n   universal_base &amp;x;\n   int f(universal_base &amp;y);\n   C(universal_base &amp;z): x(z + 1) {}\n};\n<\/code><\/pre>\n<p>The difference is that in the first construction, the expression <code>z + 1<\/code> can&#8217;t be guaranteed to be valid; you just have to tell users that <code>T<\/code> must overload <code>operator+<\/code>. In the second construction, I could add a virtual such operator to <code>universal_base<\/code>:<\/p>\n<pre><code>\/\/ in universal_base\npublic:\n virtual universal_base&amp; operator+(const universal_base &amp;x) = 0;\n<\/code><\/pre>\n<p>and use <code>typeid<\/code> and <code>dynamic_cast<\/code> in the implementations to get the argument right. This way, it is impossible to write ill-formed code, because the compiler will complain if you don&#8217;t implement <code>operator+<\/code>.<\/p>\n<p>Of course, this way it is not possible to declare members of non-reference type:<\/p>\n<pre><code>class C: public universal_base\n{\n  universal_base x; \/\/ Error: universal_base is a virtual type\n};\n<\/code><\/pre>\n<p>However, this can be gotten around through careful use of initialization. In fact, if I wanted to create a template for the above,<\/p>\n<pre><code>template \nclass C: public universal_base\n{\n  T x;\n};\n<\/code><\/pre>\n<p>I would almost certainly be giving it objects of type <code>T<\/code> at some point. In that case, there is no reason that I could not do the following:<\/p>\n<pre><code>class universal_base\n{\n  public:\n   virtual universal_base&amp; clone() = 0;\n};\n\nclass C: public universal_base\n{\n  universal_base &amp;x;\n  C(universal_base &amp;y) : x(y.clone()) {}\n}\n<\/code><\/pre>\n<p>Effectively, I create a variable of a type that is determined at runtime. This of course requires that every object of type <code>C<\/code> be appropriately initialized, but I do not think this is a huge sacrifice.<\/p>\n<p>This is not academic, since it has the following use: if I am writing a module that is intended to be linked into other programs and handle their data in some generic way, I cannot possibly know the types that will be used. Templates are not helpful in this situation, but the technique above works fine.<\/p>\n<h2>END setup<\/h2>\n<p>So, my question: does this completely replace templates, modulo the thing about initialization? Is it inefficient or dangerous somehow?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My question, which is in the last paragraph, needs (in my opinion) some explanatory setup. Basically, I&#8217;m wondering if you can avoid using templates if instead you make all your would-be template classes instead inherit from a base class that declares virtual methods you will be using, among them a function for memory allocation that, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4374","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4374","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4374"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4374\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}