I recently came across a situation in which I needed to know what a specific wrapped up function might return , after a little reading I came across the result_of Class. This simple class is nothing but short of a god gift , although boost also has an advanced version of this class but I doubt you would ever need that (At least I dont).
Anyways this class basically gives you a return type. Here is a very simple example.
Hopefully by understanding this you could construct your own examples.
Feel free to comment
using namespace std;
template <class type_a>
void Function_A(type_a a)
{
std::cout << "Non Specialized";
}
char* Function_B (int a) //Parameter does nothing
{
return "Function_B called\n";
}
template<class obj,class par>
void Analyze(obj obj_,par par_)
{
typedef typename std::result_of<obj(par)>::type new_type;
cout << "Passed Parameter:" << typeid(par_).name() << "\n";
cout << "Function Returns:" << typeid(new_type).name() << "\n";
}
int main()
{
Analyze(Function_A<double>,2.00);
Analyze(Function_B,12);
}
Anyways this class basically gives you a return type. Here is a very simple example.
Hopefully by understanding this you could construct your own examples.
Feel free to comment
using namespace std;
template <class type_a>
void Function_A(type_a a)
{
std::cout << "Non Specialized";
}
char* Function_B (int a) //Parameter does nothing
{
return "Function_B called\n";
}
template<class obj,class par>
void Analyze(obj obj_,par par_)
{
typedef typename std::result_of<obj(par)>::type new_type;
cout << "Passed Parameter:" << typeid(par_).name() << "\n";
cout << "Function Returns:" << typeid(new_type).name() << "\n";
}
int main()
{
Analyze(Function_A<double>,2.00);
Analyze(Function_B,12);
}
No comments:
Post a Comment