<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>