Chine C + + return value overload

Source: Internet
Author: User

C + +, of course, cannot simply overload a function by returning a value, but we tend to think that it would be nice to support the return value overload. Now, I'm going to find something for you from some of the most controversial corners of C + +.

Suppose you have such a function:

type getvalue(const DBField& fd);

However, the actual data type of Dbfield is not understood for GetValue, a common solution is:

template<typename T>
T getvalue(const DBField& fd);

However, this is certainly a way, and it is a good way. The problem is that sometimes it's not convenient for us to provide this t, for example, when it's an operator overload. Also, we do not want to provide this type of t when we do not intend to determine the return value type prematurely. The solution is simple and provides an indirect layer:

string getvalue(const DBField& fd);
int getvalue_int(const DBField& fd);
Result getvalue(const DBField& fd)
{
  return Result(fd);
}

See how to achieve Result:

struct Result{
  const DBField& fd;
  explicit Result(const DBField& f) : fd(f){}
  operator string() const { return getvalue_string(fd);}
  operator int() const { return getvalue_int(fd);}
};

Now let's output the data:

void print_string(const string& str){...}
void print_int(int i){...}

Use as follows:

print_string(getvalue(DBField));
print_int(getvalue(DBField));

Of course, it's not a good idea to write a type in a name, but if you like to overload, no problem:

template <typename T>
T getvalue(const DBField& fd);
struct Result{
  const DBField& fd;
  explicit Result(const DBField& f) : fd(f){}
  template<typename T>
  operator T() const { return getvalue<T>(fd);}
};

The problem with this approach is that you have to provide specific type information at an end point, which is why we write two print instead of directly using cout output. But, anyway, since you're going to overload only by returning a value, tell the code, what is the return value?

This shows the technique of lazy computing, which, through an indirect layer, delays the actual computation time to the required time. You may be dismissive of the return value overload, but this technique is very useful. Next time, I'm going to show another trick with lazy computing.

#include <iostream>
#include <string>
using namespace std;
string getvalue_slow(const int&)
{
  return "getvalue_slow";
}
string g_fast = "getvalue_fast";
const char* getvalue_fast(const int&)
{
  return g_fast.c_str();
}
struct Result
{
  const int& i;
  explicit Result(const int& r) : i(r){}
  operator string() const{ return getvalue_slow(i);}
  operator const char* () const { return getvalue_fast(i);}
};
Result getvalue(const int& i)
{
  return Result(i);
}
void print_const(const char* str)
{
  cout << str << endl;
}
void print(const string& str)
{
  cout << str << endl;
}
int main()
{
  print(getvalue(1));
  print_const(getvalue(1));
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.