mercredi 23 décembre 2015

why compilation do optimization for overloading function even when literal type is specified in C++?

I have overloaded a function with two different type arguments even when i am changing the passed arguments compiler optimized one function call void funtion(long a, long b).

#include<iostream>
class ABC
{
public:
        void funtion(long a, long b)
        {
            long &i=a;
            --i;
           //std::cout<<"long function: "<<a+b<<std::endl;
           std::cout<<"long function: "<<--i<<b<<std::endl;
        }
        void function(float a, float b)
        {
           //std::cout<<"float function"<<a+b<<std::endl;
           std::cout<<"float function"<<a<<b<<std::endl;
        }
};
int main()
{
        ABC obj;
        obj.function(1l,2l);
        obj.function(3.2f,4.2f);
        return 0;
}

Output of program

float function12
float function3.24.2

nm(symbol information) command output

ayadav@gateway1:~$ nm ./a.out |grep function
00000000004008d6 W _ZN3ABC8functionEff 

Is it right behavior of the compiler or it is hidden bug of compiler? How optimization of overloading function work? I have got similar result with below all Optimization level

None (-O0)
Moderate (-O1)
Full (-O2)
Maximum (-O3)

Aucun commentaire:

Enregistrer un commentaire