{"id":1858,"date":"2026-07-29T08:53:59","date_gmt":"2026-07-29T14:53:59","guid":{"rendered":"https:\/\/www.elbeno.com\/blog\/?p=1858"},"modified":"2026-07-29T08:53:59","modified_gmt":"2026-07-29T14:53:59","slug":"const_cast-a-necessary-evil","status":"publish","type":"post","link":"https:\/\/www.elbeno.com\/blog\/?p=1858","title":{"rendered":"const_cast: A Necessary Evil"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">We don&#8217;t like <code>const_cast<\/code> much. It&#8217;s ugly, and can even lead to undefined behaviour. But unfortunately sometimes it&#8217;s needed. Even aside from interacting with C libraries which don&#8217;t respect <code>const<\/code>, there is at least one place in the C++ standard that requires you to use it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you have a <code>std::priority_queue<\/code> of move-only objects, how do you get objects out of it? (Because It&#8217;s not a Roach Motel\u00ae, neither is it Hotel California&#8230;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>pop<\/code> function returns <code>void<\/code>. No help there.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With other container adapters like <code>std::stack<\/code> and <code>std::queue<\/code>, the <code>top<\/code>\/<code>front<\/code>\/<code>back<\/code> functions return mutable references. Those container adapters don&#8217;t have invariants to uphold that depend on the state of the contained objects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But the <code>top<\/code> function on <code>std::priority_queue<\/code> returns a reference-to-<code>const<\/code>. <code>std::priority_queue<\/code> <em>can&#8217;t<\/em> give you a mutable reference to anything inside it, because if it did that, you could alter objects inside it, and that means you could break the heap invariant.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So the only thing you <em>can<\/em> do is use <code>const_cast<\/code>, temporarily and forcibly break the invariant, and then restore it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ top can only give us a reference to const\nconst T&amp; obj = q.top();\n\n\/\/ we must cast away const to move the object out,\n\/\/ breaking the queue's invariant\nT exfiltrated_obj = std::move(const_cast&lt;T&amp;>(obj));\n\n\/\/ now we should immediately restore the queue invariant\nq.pop();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Container adapters don&#8217;t get much love in the standard: this has been broken since C++11. Hold your nose, I guess.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We don&#8217;t like const_cast much. It&#8217;s ugly, and can even lead to undefined behaviour. But unfortunately sometimes it&#8217;s needed. Even aside from interacting with C libraries which don&#8217;t respect const, there is at least one place in the C++ standard that requires you to use it. If you have a&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"class_list":["post-1858","post","type-post","status-publish","format-standard","hentry","category-cpp"],"_links":{"self":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1858","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1858"}],"version-history":[{"count":8,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1858\/revisions"}],"predecessor-version":[{"id":1866,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1858\/revisions\/1866"}],"wp:attachment":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}