[cfe-dev] "You don't pay for what you don't use" principle violation?
Jacob Carlborg
doob at me.com
Sun May 4 23:49:56 PDT 2014
On 05/05/14 00:08, Fernando Pelliccioni wrote:
> Hi all,
>
> Is there any reason why the following code produces the attached
> assembly code?
>
> struct X { int v; };
>
> int func( X** x, int n )
> {
> int sum = 0;
>
> while ( n > 0 )
> {
> sum += (*x)->v;
> ++x;
> --n;
> }
> return sum;
> }
>
> int main() {}
>
> The "func" function is not used, I think it should be omitted by the
> compiler.
I guess that's to support separate compilation. The compiler might also
need global static analyzing to determine that a function is not used.
> If I change the function to a template function
>
> template <typename T>
> int func( T** x, int n )
> {
> int sum = 0;
>
> while ( n > 0 )
> {
> sum += (*x)->v;
> ++x;
> --n;
> }
> return sum;
> }
>
> Then, no code is generated.
Templates need to be instantiate for their code to be generated. The
compiler won't event completely analyze the function unless it's
instantiated.
--
/Jacob Carlborg
More information about the cfe-dev
mailing list