{"id":4422,"date":"2014-03-30T10:50:46","date_gmt":"2014-03-30T10:50:46","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/work-around-for-being-unable-to-make-function-templates-virtual-collection-of-common-programming-errors\/"},"modified":"2014-03-30T10:50:46","modified_gmt":"2014-03-30T10:50:46","slug":"work-around-for-being-unable-to-make-function-templates-virtual-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/work-around-for-being-unable-to-make-function-templates-virtual-collection-of-common-programming-errors\/","title":{"rendered":"Work around for being unable to make function templates virtual?-Collection of common programming errors"},"content":{"rendered":"<p>Well, normally, you&#8217;re supposed to have the vertex type as a template parameter so it can be stored properly:<\/p>\n<pre><code>template\nclass vertex {\n    private:\n        T comp[NumAxes];\n};\n<\/code><\/pre>\n<p>In which case, there&#8217;s no need for a virtual method since you can just use C++&#8217;s typecasting to do the work:<\/p>\n<pre><code>template\nclass vertex {\npublic:\n    template \n    void setComp(size_t index, U value) { comp[index] = static_cast(value); }\nprivate:\n    T comp[NumAxes];\n};\n<\/code><\/pre>\n<p>Now, if you want it to be virtual because you want subclasses to be able to mess with things (e.g. log every change in value), you need to define a non-templated function:<\/p>\n<pre><code>template\nclass vertex {\npublic:\n    template \n    void setComp(size_t index, U value)\n    { _setComp(index, static_cast(value)); }\nprotected:\n    T comp[NumAxes];\n    virtual void _setComp(size_t index, T value) \n    { comp[index] = value; }\n};\n\ntemplate\nclass logged_vertex: public vertex {\nprotected:\n    virtual void _setComp(size_t index, T value);\n};\n\ntemplate\nvoid logged_vertex::_setComp(size_t index, T value)\n{   cout<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Well, normally, you&#8217;re supposed to have the vertex type as a template parameter so it can be stored properly: template class vertex { private: T comp[NumAxes]; }; In which case, there&#8217;s no need for a virtual method since you can just use C++&#8217;s typecasting to do the work: template class vertex { public: template void [&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-4422","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4422","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=4422"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4422\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}