What is std::ref useful in this function?


Martin

Why should calling std::ref be preferred over not calling it at all?

template<class F, class...Ts> F for_each_arg(F f, Ts&&...a) {
  return (void)initializer_list<int>{(ref(f)((Ts&&)a), 0)...}, f;
  // why not return (void)initializer_list<int>{(f((Ts&&)a), 0)...}, f;
}
Casey

std::reference_wrapper::operator()In some cases, the function performed is a bit more "magic" than the function of a direct function call. Its effect is specified as (quoting N4296 [refwrap.invoke]):

template <class... ArgTypes>
result_of_t<T&(ArgTypes&&... )>
operator()(ArgTypes&&... args) const;

Returns: INVOKE(get(), std::forward<ArgTypes>(args)...). (20.9.2)

where get()returns a reference to the contents of the reference_wrapperwrapper . INVOKEDescribed in 20.9.2 [func.require]:

Defined INVOKE(f, t1, t2, ..., tN)as follows:

(1.1) — (t1.*f)(t2, ..., tN)when fis a pointer to a member function of a class Tand t1is Tan object of type Tor a reference to an object of type or a reference to an object of type T;

(1.2) — ((*t1).*f)(t2, ..., tN)when fis a pointer to a member function of a class Tand t1is not one of the types described in the previous item;

(1.3) t1.*f—when N == 1and fis a pointer to member data of a class, Tand t1is an object of type or a reference to Tan object of that type Tor a reference to an object of type T;

(1.4) (*t1).*f—when N == 1sum fis a pointer to member data of a class Tand t1is not one of the types described in the previous item;

(1.5) - f(t1, t2, ..., tN)in all other cases.

A consequence of calling ref(f)rather than simply fbeing that pointers to member functions and pointers to member data can be "called" using the appropriate object pointer/reference as an argument. E.g,

struct A { void foo(); };
struct B : A {};
struct C : B {};
for_each_arg(&A::foo, A{}, B{}, C{}, std::make_unique<A>());

will be called foo, and the Atemporary object, Band Cthe held object unique_ptr( DEMO ). The reasons why you would rather use ref(f)than use obviously depend on the context in which it fis used for_each_arg.

Related


What is std::ref useful in this function?

Martin Why should calling std::ref be preferred over not calling it at all? template<class F, class...Ts> F for_each_arg(F f, Ts&&...a) { return (void)initializer_list<int>{(ref(f)((Ts&&)a), 0)...}, f; // why not return (void)initializer_list<int>{(f((Ts&&

What is the useful std::ref in this function?

Martin Why should calling std::ref be preferred over not calling it at all? template<class F, class...Ts> F for_each_arg(F f, Ts&&...a) { return (void)initializer_list<int>{(ref(f)((Ts&&)a), 0)...}, f; // why not return (void)initializer_list<int>{(f((Ts&&

What is std::ref useful in this function?

Martin Why should calling std::ref be preferred over not calling it at all? template<class F, class...Ts> F for_each_arg(F f, Ts&&...a) { return (void)initializer_list<int>{(ref(f)((Ts&&)a), 0)...}, f; // why not return (void)initializer_list<int>{(f((Ts&&

What are Python function annotations useful for?

allyourcode: I've read the first few sections of PEP 3107, but I still don't understand their benefits to the language. It seems to me that you can use a decorator to add metadata to a function. E.g def returns(return_type): f.return_type = return_type # <-

What are Python function annotations useful for?

allyourcode: I've read the first few sections of PEP 3107, but I still don't understand their benefits to the language. It seems to me that you can use a decorator to add metadata to a function. E.g def returns(return_type): f.return_type = return_type # <-

What is the difference between ( this ) and ( std::ref(*this) )

Kevin King333 I used to create a thread this way std::thread(&A::Func, this); But I found that there is another way std::thread(&A::Func, std::ref(*this)); What is the difference between them? Carles In the context of starting a thread running a class member

Is it useful to pass std::weak_ptr to a function?

ignorance I'm reading an article by Herb Sutter about passing smart pointers to functions. He didn't mention it std::weak_ptr, and honestly, I couldn't find a good scheme to pass such a smart pointer. Does the function have ownership? pass std::shared_ptr. Doe

What are the uses of the make_ref() function in elixir?

Ikichi I see a code snippet in use and am not sure about the availability of this feature.make_ref() hexdocs says: Returns almost unique references. The returned reference will reappear after about 2^82 calls; thus, it's unique enough for practical use. Inline

What are the uses of the make_ref() function in elixir?

Ikichi I see a code snippet in use and am not sure about the availability of this feature.make_ref() hexdocs says: Returns almost unique references. The returned reference will reappear after about 2^82 calls; thus, it's unique enough for practical use. Inline

What is the purpose of a Ref-qualified member function?

new version While reading http://en.cppreference.com/w/cpp/language/member_functions I came across something but never seen before: lvalue/rvalue Ref-qualified member functions. What is their purpose? kakaria Just read below: During overload resolution, if a n

What are the uses of the make_ref() function in elixir?

Ikichi I see a code snippet in use and am not sure about the availability of this feature.make_ref() hexdocs says: Returns almost unique references. The returned reference will reappear after about 2^82 calls; thus, it's unique enough for practical use. Inline

std::mem_fn with ref_qualified member function

Arun Wood Is there any way to take advantage of ref-qualified member functions std::mem_fn? The following code fails to compile: class DeadPool { public: void jump() & { std::cout << "Did not jump\n"; } void jump() && { std::cout << "Jumped from

What is the difference between std::invoke and std::function?

Ivan Kush std::invokeWhy do we need it when we already have it std::function? What is the difference between them? Both are used to invoke callable objects. What's the point of adding a new template? Jesper Juhl std::invokeis a generic way to activate any call

What happens in std::function operator() and std::forward?

Gabriel I'm looking at the std::functionimplementation and its invocationoperator() template<typename Ret, typename... ArgTypes> Ret function< Ret (ArgTypes...)>::operator()(ArgTypes...args) const { // some stuff return invoker(functor, std::forward<ArgTyp

What is the difference between std::invoke and std::function?

Ivan Kush std::invokeWhy do we need it when we already have it std::function? What is the difference between them? Both are used to invoke callable objects. What's the point of adding a new template? Jesper Juhl std::invokeis a generic way to activate any call

What is the difference between std::invoke and std::function?

Ivan Kush std::invokeWhy do we need it when we already have it std::function? What is the difference between them? Both are used to invoke callable objects. What's the point of adding a new template? Jesper Juhl std::invokeis a generic way to activate any call

What happens in std::function operator() and std::forward?

Gabriel I'm looking at the std::functionimplementation and its invocationoperator() template<typename Ret, typename... ArgTypes> Ret function< Ret (ArgTypes...)>::operator()(ArgTypes...args) const { // some stuff return invoker(functor, std::forward<ArgTyp

What happens in std::function operator() and std::forward?

Gabriel I'm looking at the std::functionimplementation and its invocationoperator() template<typename Ret, typename... ArgTypes> Ret function< Ret (ArgTypes...)>::operator()(ArgTypes...args) const { // some stuff return invoker(functor, std::forward<ArgTyp

What is the difference between std::invoke and std::function?

Ivan Kush std::invokeWhy do we need it when we already have it std::function? What is the difference between them? Both are used to invoke callable objects. What's the point of adding a new template? Jesper Juhl std::invokeis a generic way to activate any call

What is the difference between std::invoke and std::function?

Ivan Kush std::invokeWhy do we need it when we already have it std::function? What is the difference between them? Both are used to invoke callable objects. What's the point of adding a new template? Jesper Juhl std::invokeis a generic way to activate any call

What happens in std::function operator() and std::forward?

Gabriel I'm looking at the std::functionimplementation and its invocationoperator() template<typename Ret, typename... ArgTypes> Ret function< Ret (ArgTypes...)>::operator()(ArgTypes...args) const { // some stuff return invoker(functor, std::forward<ArgTyp

What happens in std::function operator() and std::forward?

Gabriel I'm looking at the std::functionimplementation and its invocationoperator() template<typename Ret, typename... ArgTypes> Ret function< Ret (ArgTypes...)>::operator()(ArgTypes...args) const { // some stuff return invoker(functor, std::forward<ArgTyp

What is the range of the function std::arg()?

Nicu Stiurca I am using the <complex> C++ header, and need to know the range of the std::arg function which returns the phase angle of a complex number. Ie, is it [-pi, pi], [0, 2*pi], or something else? Is there a guarantee about the range in the standard? AF