When is the use of std::ref necessary?

后端 未结 3 1053
[愿得一人]
[愿得一人] 2020-12-14 07:37

Consider:

std::tuple func (const A& a) 
{
  return std::make_tuple( 0 , std::ref(a) );
}

Is the std::re

3条回答
  •  情歌与酒
    2020-12-14 08:04

    std::ref does not make a reference, so in your code sample it doesn't do what you expect. std::ref creates an object that behaves similarly to a reference. It may be useful, for example, when you want to instantiate a functor, and pass a reference-like version of it to a standard library algorithm. Since algorithms take functors by value, you can use std::ref to wrap the functor.

提交回复
热议问题