
{"id":5219,"date":"2025-04-01T23:01:32","date_gmt":"2025-04-02T03:01:32","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=5219"},"modified":"2025-04-01T23:01:32","modified_gmt":"2025-04-02T03:01:32","slug":"c-regular_invocable-and-generative-ai","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=5219","title":{"rendered":"C++ regular_invocable and generative AI"},"content":{"rendered":"<p>I took interest in <a href=\"https:\/\/ikriv.com\/blog\/?p=5213\">C++ random number generation<\/a> because of the concept of <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/concepts\/invocable\">regular_invocable<\/a>. To my understanding, <code>regular_invocable<\/code> is C++&#8217;s notion of a &#8220;pure function&#8221;, or something really close to it.<\/p>\n<p>I was talking to ChatGPT about C++ concepts, and at some point asked this question:<\/p>\n<p>&#8220;<em>Does the compiler enforce regular_invocable? E.g. if I create a function that returns a random value, and then pass it to a template requiring a regular_invocable, would it cause a compilation error?<\/em>&#8221;<\/p>\n<p>ChatGPT incorrectly responded that the program below will fail to compile:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;concepts&gt;\r\n#include &lt;iostream&gt;\r\n#include &lt;random&gt;\r\n#include &lt;functional&gt;  \/\/ Include this for std::invoke\r\n\r\n\/\/ Example of a non-deterministic function\r\nint random_func(int x) {\r\n    static std::random_device rd;\r\n    static std::mt19937 gen(rd());\r\n    std::uniform_int_distribution&lt;int&gt; dis(1, 100);\r\n    return dis(gen);  \/\/ Random value returned\r\n}\r\n\r\ntemplate &lt;std::regular_invocable&lt;int&gt; F&gt;\r\nvoid test(F&amp;&amp; f) {\r\n    std::cout &lt;&lt; &quot;Result: &quot; &lt;&lt; std::invoke(std::forward&lt;F&gt;(f), 10) &lt;&lt; std::endl;\r\n}\r\n\r\nint main() {\r\n    \/\/ IVK -- the comments below, generated by ChatGPT, are NOT CORRECT -- \r\n    \/\/ The following line will cause a compilation error\r\n    \/\/ because random_func is not deterministic (it produces a random number)\r\n    test(random_func);  \/\/ Compilation error\r\n}\r\n<\/pre>\n<p>In fact, it initially responded with an example that was missing an include and a few <code>std::<\/code> snippets, but it was fixed when I complained.<\/p>\n<p>This ChatGPT response is incorrect. In reality, that compiler does not check that the function in question is pure. At least in Visual <code>std::regular_invocable<\/code> is defined as an alias of <code>std::invocable<\/code>.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/\/ C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.35.32215\\include\\concepts\r\n_EXPORT_STD template &lt;class _FTy, class... _ArgTys&gt;\r\nconcept regular_invocable = invocable&lt;_FTy, _ArgTys...&gt;;\r\n<\/pre>\n<p>Interestingly, when I asked ChatGPT this question on its own, and not in the middle of a conversation, ChatGPT gave the correct answer. So did Claude, Perplexity and Gemini. There must have been something in the context of our conversation that threw it off.<\/p>\n<p>Morals of the story:<\/p>\n<ul>\n<li>The compiler treats <code>regular_invocable<\/code> as an alias of <code>invocable<\/code>, it does not perform any &#8220;purity&#8221; checks.\/li>\n<li>Generative AI may hallucinate even about such simple and well-defined subject.\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I took interest in C++ random number generation because of the concept of regular_invocable. To my understanding, regular_invocable is C++&#8217;s notion of a &#8220;pure function&#8221;, or something really close to <a href=\"https:\/\/ikriv.com\/blog\/?p=5219\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[7],"tags":[],"class_list":["entry","author-ikriv","post-5219","post","type-post","status-publish","format-standard","category-cpp"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5219"}],"version-history":[{"count":8,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5219\/revisions"}],"predecessor-version":[{"id":5227,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5219\/revisions\/5227"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}