C++11 lambda operator namespace conflict
<p>I have three files, main.cpp, a.cpp and b.cpp. The main() function just calls a function in a.cpp, and then in b.cpp -- and I get a SIGSEGV. As far as I can tell, it looks like the lambda functions used with my sort() are conflicting with each other. Compile command line order is important; if I compile the files via:</p>

<pre><code>g++ -std=c++11 main.cpp a.cpp b.cpp
</code></pre>

<p>The code crashes (I get "<code>*** stack smashing detected ***: ./a.out terminated</code>"), but if I switch "a.cpp" and "b.cpp":</p>

<pre><code>g++ -std=c++11 main.cpp b.cpp a.cpp
</code></pre>

<p>it runs fine (I'm not saying anything about whether it "works" or not, just SIGSEGV vs no-SIGSEGV).</p>

<p>Here's the smallest code samples I could generate for the three files:</p>

<p>main.cpp:</p>

<pre><code>extern void spud1 (void);
extern void spud2 (void);

int
main (int argc, char **argv)
{
    spud1 ();
    spud2 ();
}
</code></pre>

<p>a.cpp:</p>

<pre><code>#include &lt;vector&gt;
#include &lt;string&gt;
#include &lt;algorithm&gt;

using namespace std;

struct Database
{
    int     pubdate;
    string  title;
    string  link;
};

static vector &lt;Database&gt; database;

void
spud1 (void)
{
    int     i;

    for (i = 0; i &lt; 20; i++) {
        database.push_back ({});
    }

    sort(database.begin(), database.end(),
        [] (const Database&amp; a, const Database&amp; b)
        {
            return (a.pubdate &gt; b.pubdate);
        });
}
</code></pre>

<p>b.cpp:</p>

<pre><code>#include &lt;vector&gt;
#include &lt;string&gt;
#include &lt;algorithm&gt;

using namespace std;

struct Database
{
    unsigned        serial;
    double          calories;
    double          carbs;
    double          sodium;
    double          sugar;
};

static vector &lt;Database&gt; database;

void
spud2 (void)
{
    int     i;

    for (i = 0; i &lt; 20; i++) {
        database.push_back ({});
    }

    sort(database.begin(), database.end(),
        [] (const Database&amp; a, const Database&amp; b)
        {
            return (a.serial &gt; b.serial);
        });
}
</code></pre>

<p>There are two things that bother me about this:</p>

<ol>
<li>there's no indication from the toolchain that "something bad" is happening; there are no compiler or linker warnings, and</li>
<li>I have no way to make "<code>struct Database</code>" local to the module -- if I stick "static" in front of it I get an error: "a.cpp:13:1: error: a storage class can only be specified for objects and functions**`"</li>
</ol>

<p>So, my question is, what am I doing wrong, and how can I get around it? (i.e., why is there no warning, is this "supposed" to happen? and how do I make my "Database" struct actually be local to the module? -- my workaround is to use different names, but I'm not happy with that.)</p>