Type Inference in C++ and with template : auto and decltype

 


Type Inference alludes to programmed allowance of the information kind of an articulation in a programming language. Before C++ 11, every information type should be unequivocally proclaimed at aggregate time, restricting the estimations of an articulation at runtime yet after the new form of C++, numerous watchwords are incorporated which permits a developer to leave the sort allowance to the compiler itself. 

 As all the sorts are concluded in compiler stage just, the ideal opportunity for assemblage increments marginally however it doesn't influence the run season of the program.


#include<iostream>
using namespace std;
template<class A, class B, class C>
auto Sum_num(A a,B b,C c) ->decltype(a+b+c)
{
return (a+b+c);
}
int main()
{
cout<<"Sum of number "<<Sum_num(1,2,3)<<" "<<endl;
cout<<"Sum of number "<<Sum_num(1.5,2.4,3.4)<<" "<<endl;
cout<<"Sum of number "<<Sum_num('a','b','c')<<" "<<endl;
}
view raw .cc hosted with ❤ by GitHub
Sum of number 6 
Sum of number 7.3
Sum of number 294

0 Comments