<div dir="ltr">Sorry, here the assembly code<div><br></div><div><a href="http://pastebin.com/aY8rAXyQ">http://pastebin.com/aY8rAXyQ</a><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sun, May 4, 2014 at 7:08 PM, Fernando Pelliccioni <span dir="ltr"><<a href="mailto:fpelliccioni@gmail.com" target="_blank">fpelliccioni@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Hi all,</div><div><br></div><div>Is there any reason why the following code produces the attached assembly code?</div>
<div><br></div><div> struct X { int v; };</div><div><br></div><div> int func( X** x, int n )</div>
<div> {</div><div> int sum = 0;</div><div><br></div><div> while ( n > 0 )</div><div> {</div><div> sum += (*x)->v;</div><div> ++x;</div><div> --n;</div><div> }</div>
<div> return sum;</div><div> }</div><div><br></div><div> int main() {}</div><div><br></div><div>The "func" function is not used, I think it should be omitted by the compiler.</div><div><br></div><div>
If I change the function to a template function</div><div><br></div><div> template <typename T></div><div> int func( T** x, int n )</div><div> {</div><div> int sum = 0;</div><div><br></div><div> while ( n > 0 )</div>
<div> {</div><div> sum += (*x)->v;</div><div> ++x;</div><div> --n;</div><div> }</div><div> return sum;</div><div> }</div><div><br></div><div>Then, no code is generated.</div>
<div><br></div><div><br></div><div>Compiled using:</div><div> clang++ -S -O3 -std=c++11 -S code.cpp</div><div><br></div><div>clang++ --version</div><div> clang version 3.5 (trunk 200620)</div><div> Target: x86_64-apple-darwin13.1.0</div>
<div> Thread model: posix</div><div><br></div><div><br></div><div>Best,</div><div>Fernando.</div></div>
</blockquote></div><br></div>