<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="FeedCreator 1.8" -->
<?xml-stylesheet href="https://www.baszerr.eu/lib/exe/css.php?s=feed" type="text/css"?>
<rss version="2.0">
    <channel xmlns:g="http://base.google.com/ns/1.0">
        <title>BaSzErr - blog:2014:09:24</title>
        <description></description>
        <link>https://www.baszerr.eu/</link>
        <lastBuildDate>Wed, 06 May 2026 09:37:24 +0000</lastBuildDate>
        <generator>FeedCreator 1.8</generator>
        <image>
            <url>https://www.baszerr.eu/lib/exe/fetch.php?media=wiki:dokuwiki.svg</url>
            <title>BaSzErr</title>
            <link>https://www.baszerr.eu/</link>
        </image>
        <item>
            <title>turtling_odr</title>
            <link>https://www.baszerr.eu/doku.php?id=blog:2014:09:24:turtling_odr</link>
            <description>
&lt;h1 class=&quot;sectionedit1&quot; id=&quot;turtling_odr&quot;&gt;2014-09-24 - turtling ODR&lt;/h1&gt;
&lt;div class=&quot;level1&quot;&gt;

&lt;p&gt;
in one of the project i use &lt;a href=&quot;http://turtle.sourceforge.net&quot; class=&quot;urlextern&quot; title=&quot;http://turtle.sourceforge.net&quot; rel=&quot;ugc nofollow&quot;&gt;turtle mock&lt;/a&gt; framework. after having quite a bit of experience with  &lt;a href=&quot;http://code.google.com/p/googlemock&quot; class=&quot;urlextern&quot; title=&quot;http://code.google.com/p/googlemock&quot; rel=&quot;ugc nofollow&quot;&gt;google mock&lt;/a&gt; i was a bit of a pain to get used to its compilation times – 2x longer, for simple scenarios and gets worse with size. but this was the framework of choice in the project, so i have to live with it.
&lt;/p&gt;

&lt;p&gt;
today i found another interesting thing – nice, silent way to break &lt;a href=&quot;https://en.wikipedia.org/wiki/one definition rule&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/one definition rule&quot;&gt;ODR&lt;/a&gt;. i spent few hours trying to figure out why my type, which has &lt;em&gt;operator«&lt;/em&gt; defined, does not print out in the failure case of unexpected call to mock. as one might expect there is quite a lot of macros and templates inside, so it is not trivial to “parse in mind”.
&lt;/p&gt;

&lt;p&gt;
while trying to create a minimum set, that would reproduce the problem i&amp;#039;ve noticed that commenting out a single mock method from a totally different file… i magically get my print back, as expected! my operator however worked smoothly, when called directly (say: &lt;em&gt;cout«myInstance&lt;/em&gt;). after doing a bit of digging i&amp;#039;ve noticed, that the framework does this for logging content of a failed expectations:
&lt;/p&gt;
&lt;pre class=&quot;code c&quot;&gt;namespace detail3
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
template&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt; typename S&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; typename T &lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;
S&lt;span class=&quot;sy0&quot;&gt;&amp;amp;&lt;/span&gt; operator&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt; S &lt;span class=&quot;sy0&quot;&gt;&amp;amp;&lt;/span&gt;s&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;const&lt;/span&gt; T&lt;span class=&quot;sy0&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;co1&quot;&gt;// (2)&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; s &lt;span class=&quot;sy0&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;st0&quot;&gt;&#039;?&#039;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
template&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt; typename T &lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;
stream&lt;span class=&quot;sy0&quot;&gt;&amp;amp;&lt;/span&gt; operator&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt; stream&lt;span class=&quot;sy0&quot;&gt;&amp;amp;&lt;/span&gt; s&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;const&lt;/span&gt; T&lt;span class=&quot;sy0&quot;&gt;&amp;amp;&lt;/span&gt; t &lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;co1&quot;&gt;// (1)&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  using namespace detail3&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;sy0&quot;&gt;*&lt;/span&gt;s.&lt;span class=&quot;me1&quot;&gt;s_&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; t&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; s&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
&lt;em&gt;T&lt;/em&gt; is your parameter type. at first glance it looks like a very clever thing to do: if there is an &lt;em&gt;operator«&lt;/em&gt; defined for &lt;em&gt;T&lt;/em&gt;, if will be used ((1) – Koening&amp;#039;s rule) – otherwise it will fallback to outputting &lt;em&gt;?&lt;/em&gt; on the screen (2).
&lt;/p&gt;

&lt;p&gt;
now what if, you happen to have your class &lt;em&gt;Foo&lt;/em&gt; defined in file &lt;em&gt;Foo.hpp&lt;/em&gt; but &lt;em&gt;operator«(Foo)&lt;/em&gt; in &lt;em&gt;FooOp.hpp&lt;/em&gt;? now you can have some tests including both &lt;em&gt;Foo.hpp&lt;/em&gt; and &lt;em&gt;FooOp.hpp&lt;/em&gt;, while some just &lt;em&gt;Foo.hpp&lt;/em&gt;. it all compiles and links w/o any warnings or errors. but now, the ODR is broken, as the same overload (1) calls user-defined &lt;em&gt;operator«&lt;/em&gt; in some compilation units, while overload (2) in others. since breaking ODR is an UB in C++, linker can deal with it however it pleases to. so it did.
&lt;/p&gt;

&lt;p&gt;
in my case it (probably) just took the very first implementation it came across and used it everywhere, resulting in very surprised me, having &lt;em&gt;?&lt;/em&gt; on the screen, instead of my object&amp;#039;s content, nicely formatted! the fix was to include &lt;em&gt;FooOp.hpp&lt;/em&gt; as well, in every test, that used &lt;em&gt;Foo&lt;/em&gt;. unfortunately both &lt;em&gt;Foo.hpp&lt;/em&gt; and &lt;em&gt;FooOp.hpp&lt;/em&gt; are generated so it is non-trivial to ensure they are always include, so the possible errors can lurk everywhere. :/
&lt;/p&gt;

&lt;p&gt;
even though i&amp;#039;d love to just remove problematic overload (2), i can&amp;#039;t! there is just too much code that depends on it. once it is in place, ppl use it w/o being aware of it. thanks to this we&amp;#039;re pretty much stuck with this bug-source. on the other hand, if this overload was not there in a first place, if someone would try to use custom object with mock, it would simply not compile. no way to break ODR this way + enforced pretty-printing of objects.
&lt;/p&gt;

&lt;p&gt;
lesson learned – when writing generic code, one must be extremely careful. it would be also nice to have some sort of ODR detection mechanism in place. it would cost an extra time, so it should be off by default, but if one could turn it on by a compiler/linker flag, who knows how many surprises like this could be detected? i&amp;#039;m sure my case was not alone…
&lt;/p&gt;

&lt;/div&gt;
</description>
            <author>anonymous@undisclosed.example.com (Anonymous)</author>
            <pubDate>Tue, 15 Jun 2021 20:08:54 +0000</pubDate>
        </item>
    </channel>
</rss>
