Word from the Author

"One mans mistake is another mans break"
Most of the article(s) which I have posted here are based on my own personal experience. Please do not get aggravated if you disagree with what I wrote.This is just my opinion and it might not be worth much. In case you find some errors or mistakes , have any other useful information which others including myself would benefit from or if you like an article you can always post your messages and comments here.

Using result_of Class For Managing Return Types.

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); 
}

No comments:

Post a Comment