Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any real use case for function's reference qualifiers?

Recently I learned about function's reference qualifiers, e.g.

struct foo
{
    void bar() {}
    void bar1() & {}
    void bar2() && {}
};

Where I might need this feature, is there any real use case for this language feature ?

like image 983
tommyk Avatar asked Jan 19 '15 14:01

tommyk


People also ask

What is the use of reference qualifier in ServiceNow?

Reference Qualifiers are to create filters that restrict the data that is returned for a reference field. A reference field stores a link (reference) to a field on another table, making the records/fields in the referenced table available to the form containing the reference field.

How many types of reference qualifiers does ServiceNow support?

You can use one of three types of reference qualifiers, simple, dynamic, and advanced.

What is advance reference qualifier in ServiceNow?

R eference qualifiers are a powerful tool that every ServiceNow administrator and consultant should have in their tool belt. They allow you to dynamically filter the available options from a reference field.


1 Answers

Where I might need this feature, is there any real use case for this language feature ?

The example you show is pretty useless, it's more useful when you have an overloaded function, one version that operates on lvalues and one that operates on rvalues.

Consider a type a bit like std::stringstream that owns a string and returns it by value. If the object is an rvalue, it can move the string instead of copying it.

class StringBuilder
{
public:
  std::string get() const& { return m_str; }
  std::string get() && { return std::move(m_str); }

private:
  std::string m_str;
};

This means when you return a StringBuilder from a function and want to get the string out of it, you don't need a copy:

std::string s = buildString().get();

More generally, given a function f(const X&) if it would be useful to overload it with f(X&&), then given a member function X::f() it might be useful to change it to X::f() const& and overload it with X::f()&&

like image 67
Jonathan Wakely Avatar answered Sep 19 '22 07:09

Jonathan Wakely