{"id":350,"date":"2024-09-17T18:25:54","date_gmt":"2024-09-17T18:25:54","guid":{"rendered":"https:\/\/permutationcity.co.uk\/bp\/?p=350"},"modified":"2024-09-22T20:43:43","modified_gmt":"2024-09-22T20:43:43","slug":"calling-functions","status":"publish","type":"post","link":"https:\/\/permutationcity.co.uk\/bp\/2024\/09\/17\/calling-functions\/","title":{"rendered":"Calling functions"},"content":{"rendered":"\n<p>I was recently talking about\n<a href=\"https:\/\/permutationcity.co.uk\/bp\/2024\/09\/03\/saving-pennies\/#inefficient-vector-operations\">clang-tidy<\/a>.\nOne of the performance checks it can run is\n<a href=\"https:\/\/clang.llvm.org\/extra\/clang-tidy\/checks\/performance\/unnecessary-value-param.html\">unnecessary-value-param<\/a>.\nThis makes sense,\naccidentally passing a big array by value rather than by reference can invisibly ruin the code performance.\nThe rule was pretty easy though, pass simple types by value and everything else by reference.\nThen along came <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/string\/basic_string_view\">std::string_view<\/a>\nwhich they recommend you pass by value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"function-parameters\">Function parameters<\/h2>\n\n\n\n<p>There are a few different ways of passing data to a function,\nactually more than I normally think about.\nC++ might be one of the more complex common languages here.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Value parameters are copied in.\n<ul class=\"wp-block-list\">\n<li><code>uint32_t value<\/code>: Perhaps the most common sort of parameter, fast if the value is simple.<\/li>\n\n\n\n<li><code>const uint32_t value<\/code>: More often than not the parameter is not going to change, whether it is marked in that way or not.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Reference parameters are just pointers under the hood but can be accessed as if they were values.\n<ul class=\"wp-block-list\">\n<li><code>std::string&amp; value<\/code>: This is great is you have a value you want to change but some people don&#8217;t like this because it may not be obvious that the function can change things.<\/li>\n\n\n\n<li><code>const std::string&amp; value<\/code>: Perhaps the next most common sort of parameter, fast if the value is complex but slightly less good for simple values.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Array parameters are just pointers but for a specific use case.\n<ul class=\"wp-block-list\">\n<li><code>uint32_t values[]<\/code>: An array where you can change the content.<\/li>\n\n\n\n<li><code>const uint32_t values[]<\/code>: An array you can&#8217;t change.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Pointer parameters are just values which happen to be memory addresses but that gives them extra complexity. I tend to avoid these because they can be null which requires extra error processing.\n<ul class=\"wp-block-list\">\n<li><code>char* value<\/code>: An address which you can both change directly and change what it points to.<\/li>\n\n\n\n<li><code>const char* value<\/code>: An address which you can change but not change what it points to.<\/li>\n\n\n\n<li><code>char const* value<\/code>: An address which you can&#8217;t change but can change what it points to.<\/li>\n\n\n\n<li><code>const char const* value<\/code>: An address which you can&#8217;t change or what it points to.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>That seems like a lot.\nIt&#8217;s okay if you&#8217;ve got an easy rule to follow but\nI don&#8217;t want to have to learn a lot of special cases.\nWouldn&#8217;t it be better if we could just leave it to the compiler or library developers?\nI want to be able to have a simple interface.\nAll I should have to care about is&#8230;\nwhether I want to send something <strong>in<\/strong> to the function or get something <strong>out<\/strong>.\nI wish I could just write a function like this:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">void Test(In&lt;Type&gt; input, Out&lt;Type&gt; output);<\/pre><\/div>\n\n\n\n<p>And then call it like this:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">    Test(input, output);<\/pre><\/div>\n\n\n\n<p>I want it to just work:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If <code>Type<\/code> is simple:\n<ul class=\"wp-block-list\">\n<li><code>In&lt;Type&gt;<\/code> is passed by value, lets say as a const.<\/li>\n\n\n\n<li><code>Out&lt;Type&gt;<\/code> is passed by reference.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>If <code>Type<\/code> is complex:\n<ul class=\"wp-block-list\">\n<li><code>In&lt;Type&gt;<\/code> is passed by const reference.<\/li>\n\n\n\n<li><code>Out&lt;Type&gt;<\/code> is passed by reference.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>If <code>Type<\/code> is a special case then the library developer specifies the behaviour.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"meta-programming\">Meta-programming<\/h2>\n\n\n\n<p>Simple usage of generics or templates gives you the ability to use a class or function with different types.\nHowever more powerful systems also give you the ability to manipulate and make choices based on those types.\nI think I can make my wish come true.\nUnfortunately in C++ meta-programming has a\n<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/header\/type_traits\">separate set of library functionality<\/a>\nto learn and\nI get far less practice at this.<\/p>\n\n\n\n<p>My first thought is a templated <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/type_alias\">alias declaration<\/a>.\nSomething like this:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">template &lt;typename Type, std::enable_if_t&lt;IsSimpleParameter&lt;Type&gt;, bool&gt; = true&gt;\nusing In = const Type;\n\ntemplate &lt;typename Type, std::enable_if_t&lt;IsComplexParameter&lt;Type&gt;, bool&gt; = true&gt;\nusing In = const Type&amp;;<\/pre><\/div>\n\n\n\n<p>Here <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/types\/enable_if\"><code>std::enable_if_t<\/code><\/a>\nis meant to enable \/ disable the definition based on type.\nOne should be visible for chosen for simple types and one for complex types.\nHowever the compiler complains that <code>In<\/code> already exists in the current scope.\nI also tried to specialise this for <code>std::string_view<\/code>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">template &lt;&gt;\nusing In&lt;std::string_view&gt;&gt; = const Type;<\/pre><\/div>\n\n\n\n<p>But apparently you can&#8217;t specialise an alias template in C++ yet.<\/p>\n\n\n\n<p>Next I looked at something more complicated:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">template &lt;typename Type, std::enable_if_t&lt;IsSimpleParameter&lt;Type&gt;, bool&gt; = true&gt;\nstruct In {\npublic:\n    In(const Type value) :\n        m_Value(value) {}\n\n    operator Type() {\n        return m_Value;\n    }\n\nprivate:\n    const Type m_Value;\n};<\/pre><\/div>\n\n\n\n<p>With something similar for complex types that used <code>const Type&amp;<\/code>.\nAs I had to double up on classes it was a bit repetitious.\nIt also didn&#8217;t work because\n&#8220;<code>template parameter '__formal' is incompatible with the declaration<\/code>&#8220;.\nI wish the compiler was a bit clearer about template issues.<\/p>\n\n\n\n<p>Then tried <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/types\/conditional\"><code>std::conditional_t<\/code><\/a>\nto choose between the two alternative types:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">template &lt;typename Type&gt;\nstruct In {\npublic:\n    using ParameterType = std::conditional_t&lt;IsSimpleParameter&lt;Type&gt;, const Type, const Type&amp;&gt;;\n\n    In(ParameterType value) :\n        m_Simple(value) {}\n\n    operator ParameterType() {\n        return m_Simple;\n    }\n\nprivate:\n    ParameterType m_Simple;\n};<\/pre><\/div>\n\n\n\n<p>Notionally a simple type will be copied into the class and\ncopied back out when the parameter is used in a function,\nfor a complex type it&#8217;s just a reference that is passed back and forth.\nThis actually works.\nIt automatically picks the right way to pass simple or complex parameters.<\/p>\n\n\n\n<p>Now that I&#8217;ve got it working this I realise I can simplify things:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">template &lt;typename Type&gt;\nusing In = std::conditional_t&lt;IsSimpleParameter&lt;Type&gt;, const Type, const Type&amp;&gt;;<\/pre><\/div>\n\n\n\n<p>If I use <code>In&lt;uint32_t&gt;<\/code> then it knows this is <code>const uint32_t<\/code> and\nif I use <code>In&lt;std::string&gt;<\/code> then it knows this is <code>const std::string&amp;<\/code>.<\/p>\n\n\n\n<p>I could do it more simply for <code>Out<\/code>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">template &lt;typename Type&gt;\nusing Out = Type&amp;;<\/pre><\/div>\n\n\n\n<p>You could get away without this as it&#8217;s always passing this by non-const reference, it&#8217;s not complicated.\nThere is a nice symmetry to having <code>In<\/code> and <code>Out<\/code> parameters.<\/p>\n\n\n\n<p>However maybe it&#8217;s better to use a class for this one:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">template &lt;typename Type&gt;\nstruct Out {\npublic:\n    explicit Out(Type&amp; value) :\n        m_Value(value) {}\n\n    operator Type&amp;() {\n        return m_Value;\n    }\n\n    Type&amp; operator=(const Type&amp; value) {\n        m_Value = value;\n        return m_Value;\n    }\n\nprivate:\n    Type&amp; m_Value;\n};<\/pre><\/div>\n\n\n\n<p>By using the <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/explicit\">explicit<\/a>\nkeyword we stop the compiler from using the constructor automatically.\nThis means you must mark any out parameter when you call a function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">    Test(input, Out(output));<\/pre><\/div>\n\n\n\n<p>That would help people who think a simple reference doesn&#8217;t indicate the potential change of the parameter.<\/p>\n\n\n\n<p>I haven&#8217;t talked about <code>IsSimpleParameter<\/code> yet. This is just a templated variable declaration:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">template &lt;class Type&gt;\nconstexpr bool IsSimpleParameter = sizeof(Type) &lt;= sizeof(size_t);<\/pre><\/div>\n\n\n\n<p>This means that any type that is smaller than a pointer is considered &#8220;simple&#8221; and\nwill be passed by value as an <code>In<\/code> parameter.\nAnything larger than that is &#8220;complex&#8221; and will be passed by reference as an <code>In<\/code> parameter.\nHowever this can be specialised for any type:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">template&lt;&gt;\nconstexpr bool IsSimpleParameter&lt;std::string_view&gt; = true;<\/pre><\/div>\n\n\n\n<p>So <code>std::string_view<\/code> despite taking up more space is still passed by value as in <code>In<\/code> parameter.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"meta-programming-downsides\">Meta-programming downsides<\/h2>\n\n\n\n<p>While being <em>able<\/em> to do this is great it&#8217;s not all roses.\nIt&#8217;s extra complexity.\nWriting this is harder than normal coding,\nit has different tools and the compiler&#8217;s error messages just aren&#8217;t as helpful.\nIf someone else is using it and it breaks that&#8217;s going to create problems.\nIt certainly took more time to do this than remembering that <code>std::string_view<\/code> needed some special treatment,\nat least for one library.\nIf there were lots of special cases or\nyou&#8217;re willing to commit to having your entire system like it will bring more value in the long run.<\/p>\n\n\n\n<p>There could be a slight performance hit to doing something like this. It has the potential to require extra copying and conversions. However I expect the optimizer will be able to bypass all that in almost all cases. The class based approaches don&#8217;t actually change any of the data so the final assembly code may well be unchanged.<\/p>\n\n\n\n<p>The biggest problem I had was while writing unit tests.\nI use this function to tell help determine which parameter type has been chosen:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">template &lt;typename Type&gt;\nstatic const Type* GetAddressOf(In&lt;Type&gt; value) {\n    return &amp;value;\n}<\/pre><\/div>\n\n\n\n<p>I <em>want<\/em> to be able to write these tests:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">    uint32_t integer{};\n    EXPECT_NE(GetAddressOf(integer), &amp;integer);\n\n    std::string string{};\n    EXPECT_EQ(GetAddressOf(string), &amp;string);<\/pre><\/div>\n\n\n\n<p>The integer is passed by value so the address returned from the function won&#8217;t match.\nThe string is passed by reference so the address returned from the function will match.\nHowever the compiler tells me &#8220;<code>no matching overloaded function found<\/code>&#8220;.\nIt does know that <code>GetAddressOf<\/code> is a potential match but it can&#8217;t work out what <code>Type<\/code> should be.\nThis is frustrating.\nIt is obvious to me what <code>Type<\/code> should be but not to the compiler.\nIf you tell the compiler explicitly then it&#8217;s fine:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">    uint32_t integer{};\n    EXPECT_NE(GetAddressOf&lt;uint32_t&gt;(integer), &amp;integer);\n\n    std::string string{};\n    EXPECT_EQ(GetAddressOf&lt;std::string&gt;(string), &amp;string);<\/pre><\/div>\n\n\n\n<p>So it can have problems with template functions but is fine for non-template functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"in-the-end\">In the end<\/h2>\n\n\n\n<p>Once I&#8217;d worked out that something like this <em>was<\/em> possible it was an interesting,\nif slightly frustrating, programming challenge.\nIt would be nice to have a language which treated types in the same way as other data\nso the same language constructs could be used for both.\nI have take a quick look at <a href=\"https:\/\/en.wikipedia.org\/wiki\/Lisp_(programming_language)\">Lisp<\/a> but\nit&#8217;s <em>very<\/em> different from what I&#8217;m use to.<\/p>\n\n\n\n<p>I&#8217;m not going to recommend you start using my <code>In<\/code> and <code>Out<\/code> templates, I don&#8217;t know if I&#8217;m going to use them myself. I did want to try getting an system which would &#8220;just work&#8221; with these new requirements for <code>std::string_view<\/code>. <\/p>\n\n\n\n<p>It&#8217;s not uncommon for interfaces to come with special rules: initialise this first, always remember to destroy this afterwards, and so on. Having an interface that does &#8220;just work&#8221; means developers don&#8217;t have to remember extra rules and they can&#8217;t end up getting them wrong. If you&#8217;re designing interfaces try to make them simple and reliable so that no one needs to patch them up afterwards.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was recently talking about clang-tidy. One of the performance checks it can run is unnecessary-value-param. This makes sense, accidentally passing a big array by value rather than by reference can invisibly ruin the code performance. The rule was pretty easy though, pass simple types by value and everything else by reference. Then along came [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[26,18],"class_list":["post-350","post","type-post","status-publish","format-standard","hentry","category-general","tag-languages","tag-tools"],"_links":{"self":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/350","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/comments?post=350"}],"version-history":[{"count":2,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/350\/revisions"}],"predecessor-version":[{"id":366,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/350\/revisions\/366"}],"wp:attachment":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/media?parent=350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/categories?post=350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/tags?post=350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}