<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: mdh81</title>
    <description>The latest articles on Forem by mdh81 (@mdh81).</description>
    <link>https://forem.com/mdh81</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F809718%2F9543e526-e759-4eef-89e6-6c051f19bc32.png</url>
      <title>Forem: mdh81</title>
      <link>https://forem.com/mdh81</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mdh81"/>
    <language>en</language>
    <item>
      <title>Unraveling the inline mess in C++</title>
      <dc:creator>mdh81</dc:creator>
      <pubDate>Sun, 05 Jun 2022 23:28:32 +0000</pubDate>
      <link>https://forem.com/mdh81/inline-mess-in-c-3e3a</link>
      <guid>https://forem.com/mdh81/inline-mess-in-c-3e3a</guid>
      <description>&lt;p&gt;In &lt;a href="https://dev.to/mdh81/odr-member-vs-non-member-functions-1l8b"&gt;my previous post&lt;/a&gt; about inline keyword, I loudly wondered:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I still don't know if a function that's implicitly inline is actually inlined when it's invoked. There is this c++ maxim that says "inline is only a hint and the compiler is free to ignore it". Does that apply to implicitly inlined functions? In other words, if I were to stick a 500 line definition for a function that's defined in the class definition, will the compiler still inline it?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Turns out, this is a valid question because the compiler is NOT guaranteed to inline a member functions that are present in class definitions even though defining them in the class definition &lt;em&gt;implicitly defines them as inline&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The answers in this &lt;a href="https://stackoverflow.com/questions/9192077/is-inline-implicit-in-c-member-functions-defined-in-class-definition"&gt;SO&lt;/a&gt; clearly states that it's still up to the compiler's discretion to actually inline the calls to this function. I have highlighted the relevant sections from an upvoted answer:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--btVAM6Ek--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6d2hzrsfcgnmdomono4z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--btVAM6Ek--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6d2hzrsfcgnmdomono4z.png" alt="Image description" width="880" height="601"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The last comment is more telling because it exactly talks about what was happening to the non-member friend function defined in Foo.h in the previous post. If you were to create a header only library, if you don't use the &lt;code&gt;inline&lt;/code&gt; keyword for your non-member functions, you will end up make your library useless. You won't catch this error unless you include your header in multiple sources in your tests (something that's easy to do). &lt;/p&gt;

&lt;p&gt;In short, I'm not a fan of this C++ feature. I see SO responses that go to great lengths to explain the feature, but I don't think even the gurus can point to this feature as a strength of the language.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>inline</category>
    </item>
    <item>
      <title>ODR: Member vs non-member functions</title>
      <dc:creator>mdh81</dc:creator>
      <pubDate>Sun, 05 Jun 2022 22:09:10 +0000</pubDate>
      <link>https://forem.com/mdh81/odr-member-vs-non-member-functions-1l8b</link>
      <guid>https://forem.com/mdh81/odr-member-vs-non-member-functions-1l8b</guid>
      <description>&lt;p&gt;I have always found &lt;code&gt;inline&lt;/code&gt; keyword in C++ to be a confusing and poorly designed concept. Here is an additional use-case for inline that's designed to obey the one-definition-rule (ODR).&lt;/p&gt;

&lt;p&gt;A common C++ operation is to overload the stream insertion operator so you can print your custom types with the expression &lt;code&gt;output stream &amp;lt;&amp;lt; &amp;lt;instance of my type&amp;gt;;&lt;/code&gt; The correct definition for such an overload is this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#ifndef FOO_H
#define FOO_H

#include &amp;lt;iostream&amp;gt;


class Foo {
    public:
        Foo() { m_id = 0; }
    private:
        int m_id;

    friend std::ostream&amp;amp; operator&amp;lt;&amp;lt;(std::ostream&amp;amp; os, Foo&amp;amp; f);

};

std::ostream&amp;amp; operator&amp;lt;&amp;lt;(std::ostream&amp;amp; os, const Foo&amp;amp; f) {
    os &amp;lt;&amp;lt; f.m_id &amp;lt;&amp;lt; std::endl;
    return os;
} 


#endif
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if in a source file you do &lt;code&gt;cout &amp;lt;&amp;lt; Foo()&lt;/code&gt;, the compiler will happily resolve the call to the friend function overload above.&lt;/p&gt;

&lt;p&gt;But, what happens when you include Foo.h in two different source files? You run into the dreaded multiple-definitions error for the function &lt;code&gt;operator &amp;lt;&amp;lt; (ostream&amp;amp;, const Foo&amp;amp;)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There are two ways to fix this, create a Foo.cpp and move the definition of &lt;code&gt;operator &amp;lt;&amp;lt; (ostream&amp;amp;, const Foo&amp;amp;)&lt;/code&gt; to that source file and then to build that source file with the rest of your program. The other option is to keep the function definition in the header file but with the addition of the keyword &lt;code&gt;inline&lt;/code&gt; like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;inline std::ostream&amp;amp; operator&amp;lt;&amp;lt;(std::ostream&amp;amp; os, const Foo&amp;amp; f) {
    os &amp;lt;&amp;lt; f.m_id &amp;lt;&amp;lt; std::endl;
    return os;
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The presence of the &lt;code&gt;inline&lt;/code&gt; keyword tells the compiler to use this definition found in the header file in every translation unit (a source file) where this function is used.  &lt;/p&gt;

&lt;p&gt;In essence, if you have a function defined in a header file, it better be an inline function, otherwise when multiple source files include it, you will have multiple definitions of that function that run afoul of the ODR rule. &lt;/p&gt;

&lt;p&gt;Now, you might wonder why the same doesn't apply &lt;em&gt;to member functions defined in the header file&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For example, let's say we add a new member function called &lt;code&gt;getId()&lt;/code&gt; to the &lt;code&gt;Foo&lt;/code&gt; class like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#ifndef FOO_H
#define FOO_H

#include &amp;lt;iostream&amp;gt;


class Foo {
    public:
        Foo() { m_id = 0; }
        int getId() { return m_id; }
    private:
        int m_id;

    friend std::ostream&amp;amp; operator&amp;lt;&amp;lt;(std::ostream&amp;amp; os, Foo&amp;amp; f);

};

inline std::ostream&amp;amp; operator&amp;lt;&amp;lt;(std::ostream&amp;amp; os, const Foo&amp;amp; f) {
    os &amp;lt;&amp;lt; f.m_id &amp;lt;&amp;lt; std::endl;
    return os;
} 


#endif
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, one would naturally wonder if two translation units calling &lt;code&gt;getId()&lt;/code&gt; would get two definitions of &lt;code&gt;getId()&lt;/code&gt; since the same happened when we had &lt;code&gt;operator &amp;lt;&amp;lt;(ostream&amp;amp;, const Foo&amp;amp;)&lt;/code&gt; defined in the &lt;code&gt;Foo.h&lt;/code&gt; and when two source files included &lt;code&gt;Foo.h&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;But, this is not the case--you can include &lt;code&gt;Foo.h&lt;/code&gt; in any number of source files and all of them resolve to the single definition of &lt;code&gt;getId()&lt;/code&gt; defined in &lt;code&gt;Foo.h&lt;/code&gt;. Why is this? In their infinite wisdom C++ designers choose to &lt;em&gt;implicitly inline all member functions defined in the class definition&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I still don't know if a function that's implicitly inline is actually inlined when it's invoked. There is this c++ maxim that says "inline is only a hint and the compiler is free to ignore it". Does that apply to implicitly inlined functions? In other words, if I were to stick a 500 line definition for a function that's defined in the class definition, will the compiler still inline it? &lt;/p&gt;

&lt;p&gt;There you have it. If you find &lt;code&gt;inline&lt;/code&gt; keyword confusing, you're not alone. It makes no sense to me to make a language feature so unintuitive.  &lt;/p&gt;

</description>
      <category>cpp</category>
      <category>odr</category>
      <category>onedefinitionrule</category>
      <category>cpprules</category>
    </item>
    <item>
      <title>C++ Concepts In Action</title>
      <dc:creator>mdh81</dc:creator>
      <pubDate>Sun, 15 May 2022 05:23:16 +0000</pubDate>
      <link>https://forem.com/mdh81/c-concepts-in-action-2739</link>
      <guid>https://forem.com/mdh81/c-concepts-in-action-2739</guid>
      <description>&lt;p&gt;C++20 has a new feature called Concept that allows developers to specify constraints on template parameters. Template meta programming is a complex topic that stumps even seasoned veterans. I found an interesting usage of this feature to ensure that I can write an overloaded stream insertion operator that can output C++ containers that support range based iteration (basically types that have begin and end methods).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;unordered_map&amp;gt;
#include &amp;lt;array&amp;gt;
#include &amp;lt;vector&amp;gt;
using namespace std;

template&amp;lt;typename ContainerType&amp;gt;
concept SupportsRangeBasedFor = requires(ContainerType container) {
    container.begin();
    container.end();
};

template&amp;lt;typename KeyType, typename ValueType&amp;gt;
ostream&amp;amp; operator&amp;lt;&amp;lt;(ostream&amp;amp; os, const pair&amp;lt;KeyType, ValueType&amp;gt;&amp;amp; p) {
    os &amp;lt;&amp;lt; p.first &amp;lt;&amp;lt; "-&amp;gt;" &amp;lt;&amp;lt; p.second;
    return os;
}

ostream&amp;amp; operator&amp;lt;&amp;lt;(ostream&amp;amp; os, SupportsRangeBasedFor auto&amp;amp;&amp;amp; container) {
    os &amp;lt;&amp;lt; "[ ";
    for (const auto&amp;amp; element : container) {
        os &amp;lt;&amp;lt; element &amp;lt;&amp;lt; ' ';
    }
    os &amp;lt;&amp;lt; ']' &amp;lt;&amp;lt; endl;
    return os;
}

int main()
{
    cout &amp;lt;&amp;lt; vector&amp;lt;int&amp;gt;{1,2,3} &amp;lt;&amp;lt; endl;
    cout &amp;lt;&amp;lt; array&amp;lt;int,3&amp;gt;{4,5,6} &amp;lt;&amp;lt; endl;
    cout &amp;lt;&amp;lt; unordered_map&amp;lt;int,int&amp;gt;{{8,9},{10,11},{11,12}} &amp;lt;&amp;lt; endl;
    //struct Foo {};
    //cout &amp;lt;&amp;lt; Foo {} &amp;lt;&amp;lt; endl; // Compiler Error: No overload for operator&amp;lt;&amp;lt; that accepts Foo
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running this program produces this output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;~/code/cpp/concepts &amp;gt; ./concept
[ 1 2 3 ]

[ 4 5 6 ]

[ 11-&amp;gt;12 10-&amp;gt;11 8-&amp;gt;9 ]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, we have a single overload that can support any container that has a begin and end method. This is not magic, you could have got this to work without &lt;code&gt;SupportsRangeBasedFor&lt;/code&gt; concept, but the magic is what the concept prevents from happening: &lt;/p&gt;

&lt;p&gt;Without the constraint on the template parameter, compiler would have happily chosen &lt;code&gt;operator&amp;lt;&amp;lt;&lt;/code&gt; overload for &lt;code&gt;Foo&lt;/code&gt; and you would get an indecipherable blob of errors caused by the fact that &lt;code&gt;Foo&lt;/code&gt; doesn't have &lt;code&gt;begin&lt;/code&gt; and &lt;code&gt;end&lt;/code&gt; methods. You will see errors of this nature:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;concept.cpp:22:30: error: invalid range expression of type 'Foo'; no viable 'begin' function available
    for (const auto&amp;amp; element : container) {
                             ^ ~~~~~~~~~
concept.cpp:35:10: note: in instantiation of function template specialization 'operator&amp;lt;&amp;lt;&amp;lt;Foo&amp;gt;' requested here
    cout &amp;lt;&amp;lt; Foo {} &amp;lt;&amp;lt; endl; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not only that, the compiler would match any &lt;code&gt;ostream.operator&amp;lt;&amp;lt;(...)&lt;/code&gt; call to this overload because the template parameter type (introduced by &lt;code&gt;auto&lt;/code&gt; type specifier) will match all types. &lt;/p&gt;

&lt;p&gt;The type constraint expressed by the concept explicitly forbids the compiler from picking up your template function for types that you don't wish to support. This is a pretty neat feature and I hope to be using this quite widely in my future development to limit the types that my implementation should be chosen for :)&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>cpp20</category>
      <category>templates</category>
    </item>
    <item>
      <title>Tab/window management with titles</title>
      <dc:creator>mdh81</dc:creator>
      <pubDate>Thu, 21 Apr 2022 04:16:12 +0000</pubDate>
      <link>https://forem.com/mdh81/tabwindow-management-with-titles-2db8</link>
      <guid>https://forem.com/mdh81/tabwindow-management-with-titles-2db8</guid>
      <description>&lt;p&gt;Do you get lost managing multiple tabs or windows in your terminal emulator? If so, this trick might be handy to your shell toolbox. &lt;/p&gt;

&lt;p&gt;Define a function like this in your shell profile&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function title {
    echo -ne "\033]0;"$*"\007"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then when you open your all important terminal window or tab, call the function and pass a string to it like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ title &amp;lt;title string&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Viola, you have a handy way of identifying your terminal window/tab and not be lost in a forest of tabs or windows. &lt;/p&gt;

&lt;p&gt;Isn't this neat?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fajh4xdc1mhx8eb1bg15c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fajh4xdc1mhx8eb1bg15c.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's explain this function a bit. &lt;code&gt;echo&lt;/code&gt; needs no introduction. &lt;code&gt;-n&lt;/code&gt; stands tells echo not to print a new-line after the message. &lt;code&gt;-e&lt;/code&gt;tells echo to interpret escape sequences. &lt;/p&gt;

&lt;p&gt;The trick that makes this work is the text flanked by xterm escape sequences and sequences themselves.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;\033&lt;/code&gt; Escape&lt;br&gt;
&lt;code&gt;\077&lt;/code&gt; Bell&lt;br&gt;
&lt;code&gt;]0;&lt;/code&gt;  Identifier for window title and icon&lt;br&gt;
&lt;code&gt;"$*"&lt;/code&gt; String that contains all arguments passed to echo&lt;/p&gt;

&lt;p&gt;Most terminal emulators support these xterm sequences. I tested in iterm and it works beautifully. Go ahead, try it out and help yourself navigate the terminal tab/window maze.&lt;/p&gt;

&lt;p&gt;References:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://tldp.org/HOWTO/Xterm-Title-3.html" rel="noopener noreferrer"&gt;https://tldp.org/HOWTO/Xterm-Title-3.html&lt;/a&gt;&lt;br&gt;
Stackoverflow threads on this topic.&lt;/p&gt;

</description>
      <category>shell</category>
      <category>iterm2</category>
      <category>misc</category>
      <category>unix</category>
    </item>
    <item>
      <title>Integer to void* conversion</title>
      <dc:creator>mdh81</dc:creator>
      <pubDate>Sun, 10 Apr 2022 15:36:41 +0000</pubDate>
      <link>https://forem.com/mdh81/integral-to-void-conversion-o3c</link>
      <guid>https://forem.com/mdh81/integral-to-void-conversion-o3c</guid>
      <description>&lt;p&gt;I came across this intriguing OpenGL code snippet&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;glVertexAttribPointer(colorAttrib,              
                      1,                        
                      GL_FLOAT,                 
                      GL_FALSE,                 
                      3*sizeof(float),          
                      (void*)(2*sizeof(float)) // FTW?!
                      );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;glVertexAttribPointer's signature says the last argument is of type &lt;code&gt;void*&lt;/code&gt;. This argument is supposed to tell OpenGL what the offset is into a vertex buffer array data. &lt;/p&gt;

&lt;p&gt;This looked surely wrong to me--how the hell can you cast an integral type to a pointer and use that pointer meaningfully? To demonstrate that this doesn't compute, I wrote a simple program like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
using namespace std;


void fooFunc(void* ptr) {
    int* intptr = reinterpret_cast&amp;lt;int*&amp;gt;(ptr);
    cout &amp;lt;&amp;lt; *intptr &amp;lt;&amp;lt; endl; // **Crashes here as expected**
}


int main() {
    fooFunc((void*)(12*3));
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sure enough, it crashes when it tries to de-reference the  int pointer because the void* that the int* is converted from is pointing to garbage memory.&lt;/p&gt;

&lt;p&gt;While it was clear that it was garbage memory, it wasn't immediately clear to me how the value of that pointer could be used. Understanding that was the key to unlocking how this awkward OpenGL API was working.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;fooFunc&lt;/code&gt;, &lt;code&gt;ptr&lt;/code&gt;'s value was 0x24. Clearly, garbage memory. But, it's also the result of the integer expression 12*3 from which the pointer was created in the calling code! Now, how can this be useful for anyone?!&lt;/p&gt;

&lt;p&gt;Consider this new version of &lt;code&gt;fooFunc&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void fooFunc(void* ptr) {
    size_t intval = reinterpret_cast&amp;lt;size_t&amp;gt;(ptr);
    cout &amp;lt;&amp;lt; intval &amp;lt;&amp;lt; endl; // ** prints 36 **
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we have managed to make something out of the dangling void pointer! We have managed to get the integral value that the caller wanted to pass to this function without passing an integral value, instead the caller passed a pointer!&lt;/p&gt;

&lt;p&gt;Now, why can't glVertexAttribPointer simply use an integer parameter type and save us the trouble of staring hard at this mysterious looking code? The answer, like it most often is in cases like this is--legacy. Apparently, there was time when OpenGL would allow glVertexAttribPointer()'s last parameter to be used to specify a pointer to the vertex data array in the CPU memory. Post VBOs, we transfer the vertex data to the GPU via glBufferData() call and don't need to pass it via glVertexAttribPointer() call. The Khronos group decided to make this API backward compatible and decided to overload the last parameter to allow the user to set an offset into vertex buffer array data. So, we ended up with this awkward &lt;strong&gt;(void*)(2*sizeof(float)&lt;/strong&gt; cast from integer to pointer.&lt;/p&gt;

&lt;p&gt;References:&lt;br&gt;
&lt;a href="https://stackoverflow.com/questions/28063307/in-opengl-why-does-glvertexattribpointer-require-the-pointer-argument-to-be-p"&gt;https://stackoverflow.com/questions/28063307/in-opengl-why-does-glvertexattribpointer-require-the-pointer-argument-to-be-p&lt;/a&gt;&lt;br&gt;
&lt;a href="https://stackoverflow.com/questions/58679610/how-to-cast-an-integer-to-a-void-without-violating-c-core-guidelines"&gt;https://stackoverflow.com/questions/58679610/how-to-cast-an-integer-to-a-void-without-violating-c-core-guidelines&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Answer: C++ inlining class methods causes undefined reference</title>
      <dc:creator>mdh81</dc:creator>
      <pubDate>Wed, 23 Feb 2022 07:01:53 +0000</pubDate>
      <link>https://forem.com/mdh81/answer-c-inlining-class-methods-causes-undefined-reference-27di</link>
      <guid>https://forem.com/mdh81/answer-c-inlining-class-methods-causes-undefined-reference-27di</guid>
      <description>&lt;div class="ltag__stackexchange--container"&gt;
  &lt;div class="ltag__stackexchange--title-container"&gt;
    
      &lt;div class="ltag__stackexchange--title"&gt;
        &lt;h1&gt;
          &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7Gn-iPj_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/stackoverflow-logo-b42691ae545e4810b105ee957979a853a696085e67e43ee14c5699cf3e890fb4.svg" alt=""&gt;
            &lt;a href="https://stackoverflow.com/questions/4769479/c-inlining-class-methods-causes-undefined-reference/4769571#4769571" rel="noopener noreferrer"&gt;
              &lt;span class="title-flare"&gt;answer&lt;/span&gt; re: C++ inlining class methods causes undefined reference
            &lt;/a&gt;
        &lt;/h1&gt;
        &lt;div class="ltag__stackexchange--post-metadata"&gt;
          &lt;span&gt;Jan 22 '11&lt;/span&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;a class="ltag__stackexchange--score-container" href="https://stackoverflow.com/questions/4769479/c-inlining-class-methods-causes-undefined-reference/4769571#4769571" rel="noopener noreferrer"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y9mJpuJP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/stackexchange-arrow-up-eff2e2849e67d156181d258e38802c0b57fa011f74164a7f97675ca3b6ab756b.svg" alt=""&gt;
        &lt;div class="ltag__stackexchange--score-number"&gt;
          19
        &lt;/div&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wif5Zq3z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/stackexchange-arrow-down-4349fac0dd932d284fab7e4dd9846f19a3710558efde0d2dfd05897f3eeb9aba.svg" alt=""&gt;
      &lt;/a&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--body"&gt;
    
&lt;p&gt;7.1.2/4 of the Standard:&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;An inline function shall be defined in
  every translation unit in which it is
  used...&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You use TestMethod in main.cpp, but it's not defined there.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;... If a function with external linkage is
  declared inline in one translation
  unit, it shall be declared inline in
  all…&lt;/p&gt;
&lt;/blockquote&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--btn--container"&gt;
    
      &lt;a href="https://stackoverflow.com/questions/4769479/c-inlining-class-methods-causes-undefined-reference/4769571#4769571" rel="noopener noreferrer"&gt;Open Full Answer&lt;/a&gt;
    
  &lt;/div&gt;
&lt;/div&gt;


</description>
    </item>
    <item>
      <title>Answer: MacOSX: which dynamic libraries linked by binary?</title>
      <dc:creator>mdh81</dc:creator>
      <pubDate>Tue, 22 Feb 2022 07:05:01 +0000</pubDate>
      <link>https://forem.com/mdh81/answer-macosx-which-dynamic-libraries-linked-by-binary-4nfi</link>
      <guid>https://forem.com/mdh81/answer-macosx-which-dynamic-libraries-linked-by-binary-4nfi</guid>
      <description>&lt;div class="ltag__stackexchange--container"&gt;
  &lt;div class="ltag__stackexchange--title-container"&gt;
    
      &lt;div class="ltag__stackexchange--title"&gt;
        &lt;h1&gt;
          &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7Gn-iPj_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/stackoverflow-logo-b42691ae545e4810b105ee957979a853a696085e67e43ee14c5699cf3e890fb4.svg" alt=""&gt;
            &lt;a href="https://stackoverflow.com/questions/45464584/macosx-which-dynamic-libraries-linked-by-binary/56248596#56248596" rel="noopener noreferrer"&gt;
              &lt;span class="title-flare"&gt;answer&lt;/span&gt; re: MacOSX: which dynamic libraries linked by binary?
            &lt;/a&gt;
        &lt;/h1&gt;
        &lt;div class="ltag__stackexchange--post-metadata"&gt;
          &lt;span&gt;May 22 '19&lt;/span&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;a class="ltag__stackexchange--score-container" href="https://stackoverflow.com/questions/45464584/macosx-which-dynamic-libraries-linked-by-binary/56248596#56248596" rel="noopener noreferrer"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y9mJpuJP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/stackexchange-arrow-up-eff2e2849e67d156181d258e38802c0b57fa011f74164a7f97675ca3b6ab756b.svg" alt=""&gt;
        &lt;div class="ltag__stackexchange--score-number"&gt;
          10
        &lt;/div&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wif5Zq3z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/stackexchange-arrow-down-4349fac0dd932d284fab7e4dd9846f19a3710558efde0d2dfd05897f3eeb9aba.svg" alt=""&gt;
      &lt;/a&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--body"&gt;
    
&lt;p&gt;Rob Mayoff's answer is a great solution when working with executables. If you find you need to check the runtime dependencies of a dylib, the following script &lt;code&gt;my-ldd&lt;/code&gt; may be useful.&lt;/p&gt;
&lt;pre class="lang-sh prettyprint-override"&gt;&lt;code&gt;#!/usr/bin/env bash 

while getopts "r" OPTION; do
  case $OPTION in   
    r) export DYLD_PRINT_RPATHS=1;;
  esac
done
shift $((OPTIND-1))

cp `which&lt;/code&gt;&lt;/pre&gt;…
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--btn--container"&gt;
    
      &lt;a href="https://stackoverflow.com/questions/45464584/macosx-which-dynamic-libraries-linked-by-binary/56248596#56248596" rel="noopener noreferrer"&gt;Open Full Answer&lt;/a&gt;
    
  &lt;/div&gt;
&lt;/div&gt;


</description>
    </item>
    <item>
      <title>Answer: How to initialize private static members in C++?</title>
      <dc:creator>mdh81</dc:creator>
      <pubDate>Sun, 06 Feb 2022 03:23:53 +0000</pubDate>
      <link>https://forem.com/mdh81/answer-how-to-initialize-private-static-members-in-c-4pmg</link>
      <guid>https://forem.com/mdh81/answer-how-to-initialize-private-static-members-in-c-4pmg</guid>
      <description>&lt;div class="ltag__stackexchange--container"&gt;
  &lt;div class="ltag__stackexchange--title-container"&gt;
    
      &lt;div class="ltag__stackexchange--title"&gt;
        &lt;h1&gt;
          &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7Gn-iPj_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/stackoverflow-logo-b42691ae545e4810b105ee957979a853a696085e67e43ee14c5699cf3e890fb4.svg" alt=""&gt;
            &lt;a href="https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c/185848#185848" rel="noopener noreferrer"&gt;
              &lt;span class="title-flare"&gt;answer&lt;/span&gt; re: How to initialize private static members in C++?
            &lt;/a&gt;
        &lt;/h1&gt;
        &lt;div class="ltag__stackexchange--post-metadata"&gt;
          &lt;span&gt;Oct  9 '08&lt;/span&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;a class="ltag__stackexchange--score-container" href="https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c/185848#185848" rel="noopener noreferrer"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y9mJpuJP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/stackexchange-arrow-up-eff2e2849e67d156181d258e38802c0b57fa011f74164a7f97675ca3b6ab756b.svg" alt=""&gt;
        &lt;div class="ltag__stackexchange--score-number"&gt;
          617
        &lt;/div&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wif5Zq3z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/stackexchange-arrow-down-4349fac0dd932d284fab7e4dd9846f19a3710558efde0d2dfd05897f3eeb9aba.svg" alt=""&gt;
      &lt;/a&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--body"&gt;
    
&lt;p&gt;The class declaration should be in the header file (Or in the source file if not shared).&lt;br&gt;
File: foo.h&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;class foo
{
    private:
        static int i;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But the initialization should be in source file.&lt;br&gt;
File: foo.cpp&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;int foo::i = 0;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If the initialization is in the header file then…&lt;/p&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--btn--container"&gt;
    
      &lt;a href="https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c/185848#185848" rel="noopener noreferrer"&gt;Open Full Answer&lt;/a&gt;
    
  &lt;/div&gt;
&lt;/div&gt;


</description>
    </item>
    <item>
      <title>Answer: Am I using the copy_if wrong?</title>
      <dc:creator>mdh81</dc:creator>
      <pubDate>Sat, 05 Feb 2022 23:15:11 +0000</pubDate>
      <link>https://forem.com/mdh81/answer-am-i-using-the-copyif-wrong-51md</link>
      <guid>https://forem.com/mdh81/answer-am-i-using-the-copyif-wrong-51md</guid>
      <description>&lt;div class="ltag__stackexchange--container"&gt;
  &lt;div class="ltag__stackexchange--title-container"&gt;
    
      &lt;div class="ltag__stackexchange--title"&gt;
        &lt;h1&gt;
          &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7Gn-iPj_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/stackoverflow-logo-b42691ae545e4810b105ee957979a853a696085e67e43ee14c5699cf3e890fb4.svg" alt=""&gt;
            &lt;a href="https://stackoverflow.com/questions/5432739/am-i-using-the-copy-if-wrong/5432774#5432774" rel="noopener noreferrer"&gt;
              &lt;span class="title-flare"&gt;answer&lt;/span&gt; re: Am I using the copy_if wrong?
            &lt;/a&gt;
        &lt;/h1&gt;
        &lt;div class="ltag__stackexchange--post-metadata"&gt;
          &lt;span&gt;Mar 25 '11&lt;/span&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;a class="ltag__stackexchange--score-container" href="https://stackoverflow.com/questions/5432739/am-i-using-the-copy-if-wrong/5432774#5432774" rel="noopener noreferrer"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y9mJpuJP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/stackexchange-arrow-up-eff2e2849e67d156181d258e38802c0b57fa011f74164a7f97675ca3b6ab756b.svg" alt=""&gt;
        &lt;div class="ltag__stackexchange--score-number"&gt;
          64
        &lt;/div&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wif5Zq3z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/stackexchange-arrow-down-4349fac0dd932d284fab7e4dd9846f19a3710558efde0d2dfd05897f3eeb9aba.svg" alt=""&gt;
      &lt;/a&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--body"&gt;
    
&lt;p&gt;The copy_if algorithm looks something like this(taken from MSVC2010): &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;template&amp;lt;class InIt, class OutIt, class Pr&amp;gt; inline
OutIt copy_if(InIt First, InIt Last, OutIt Dest, Pr Pred)
{
    for (; First != _Last; ++First)
        if (Pred(*_First))
            *Dest++ = *First;
    return (Dest);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And as you can see the copy_if does not do…&lt;/p&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--btn--container"&gt;
    
      &lt;a href="https://stackoverflow.com/questions/5432739/am-i-using-the-copy-if-wrong/5432774#5432774" rel="noopener noreferrer"&gt;Open Full Answer&lt;/a&gt;
    
  &lt;/div&gt;
&lt;/div&gt;


</description>
    </item>
    <item>
      <title>Answer: Why would one use nested classes in C++?</title>
      <dc:creator>mdh81</dc:creator>
      <pubDate>Sat, 05 Feb 2022 06:16:55 +0000</pubDate>
      <link>https://forem.com/mdh81/answer-why-would-one-use-nested-classes-in-c-eam</link>
      <guid>https://forem.com/mdh81/answer-why-would-one-use-nested-classes-in-c-eam</guid>
      <description>&lt;div class="ltag__stackexchange--container"&gt;
  &lt;div class="ltag__stackexchange--title-container"&gt;
    
      &lt;div class="ltag__stackexchange--title"&gt;
        &lt;h1&gt;
          &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7Gn-iPj_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/stackoverflow-logo-b42691ae545e4810b105ee957979a853a696085e67e43ee14c5699cf3e890fb4.svg" alt=""&gt;
            &lt;a href="https://stackoverflow.com/questions/4571355/why-would-one-use-nested-classes-in-c/4571683#4571683" rel="noopener noreferrer"&gt;
              &lt;span class="title-flare"&gt;answer&lt;/span&gt; re: Why would one use nested classes in C++?
            &lt;/a&gt;
        &lt;/h1&gt;
        &lt;div class="ltag__stackexchange--post-metadata"&gt;
          &lt;span&gt;Dec 31 '10&lt;/span&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;a class="ltag__stackexchange--score-container" href="https://stackoverflow.com/questions/4571355/why-would-one-use-nested-classes-in-c/4571683#4571683" rel="noopener noreferrer"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y9mJpuJP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/stackexchange-arrow-up-eff2e2849e67d156181d258e38802c0b57fa011f74164a7f97675ca3b6ab756b.svg" alt=""&gt;
        &lt;div class="ltag__stackexchange--score-number"&gt;
          262
        &lt;/div&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wif5Zq3z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/stackexchange-arrow-down-4349fac0dd932d284fab7e4dd9846f19a3710558efde0d2dfd05897f3eeb9aba.svg" alt=""&gt;
      &lt;/a&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--body"&gt;
    
&lt;p&gt;Nested classes are cool for hiding implementation details.&lt;/p&gt;
&lt;p&gt;List:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class List
{
    public:
        List(): head(nullptr), tail(nullptr) {}
    private:
        class Node
        {
              public:
                  int   data;
                  Node* next;
                  Node* prev;
        };
    private:
        Node*     head;
        Node*     tail;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here I don't want to expose Node as other people may decide to use the…&lt;/p&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--btn--container"&gt;
    
      &lt;a href="https://stackoverflow.com/questions/4571355/why-would-one-use-nested-classes-in-c/4571683#4571683" rel="noopener noreferrer"&gt;Open Full Answer&lt;/a&gt;
    
  &lt;/div&gt;
&lt;/div&gt;


</description>
    </item>
  </channel>
</rss>
