{"id":433,"date":"2022-08-30T15:01:16","date_gmt":"2022-08-30T15:01:16","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/c-void-non-pointer-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:01:16","modified_gmt":"2022-08-30T15:01:16","slug":"c-void-non-pointer-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/c-void-non-pointer-collection-of-common-programming-errors\/","title":{"rendered":"C++ Void non-pointer-Collection of common programming errors"},"content":{"rendered":"<p>In a strongly typed language, all data has a type, so there is no concept of &#8220;void&#8221; as a datatype. In C and C++, it&#8217;s meanings are either &#8220;no data&#8221; (as a return value), or &#8220;data whose type you don&#8217;t know&#8221; (as a pointer target).<\/p>\n<p>You are proposing an in-between state of &#8220;data whose type you don&#8217;t know but whose size you do&#8221;, presumably as an opaque version of a type you pass by value. Besides being rather a dangerous thing to do (since you have to manually update all the code using the opaque type when you change the size of the real type), this can already be done without adding weird extensions to the language, for example:<\/p>\n<pre><code>struct void8 { char bytes[8]; };\n<\/code><\/pre>\n<p>This <em>could<\/em> be used in conjunction with <code>reinterpret_cast<\/code> to pass POD types around as opaque lumps, in much the way you suggest. Using it with non-POD types is even more likely to invoke undefined behaviour than using <code>void*<\/code> to refer to non-POD types.<\/p>\n<p>It would be safer and more idiomatic to handle opaque types as pointers or references to named types that have been declared but not defined, or something like <code>boost::any<\/code> if you want value semantics. There&#8217;s rarely a need to use &#8220;void&#8221; as a placeholder type in C++.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 19:42:20. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>In a strongly typed language, all data has a type, so there is no concept of &#8220;void&#8221; as a datatype. In C and C++, it&#8217;s meanings are either &#8220;no data&#8221; (as a return value), or &#8220;data whose type you don&#8217;t know&#8221; (as a pointer target). You are proposing an in-between state of &#8220;data whose type [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,1],"tags":[],"class_list":["post-433","post","type-post","status-publish","format-standard","hentry","category-semantic","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/433","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=433"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/433\/revisions"}],"predecessor-version":[{"id":8815,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/433\/revisions\/8815"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}