c++ - Control to what kind of references `T` binds -


while thinking can done solve std::min dangling reference problem, 1 thought had add overload (actually 3 - each combination) rvalues deleted. issue t&& forwarding reference, not rvalue reference.

i want separate question std::min , make more general. std::min can taken example why need such thing.

lets simplify , generalize problem:

// has same problem `std::min`: if t binds temporary, // , result assigned `auto&`, result dangled reference template <class t> const t& foo(const t& t) {   return t; }  // incorrect attempt prevent foo being called temporary argument // `t&&` forwarding reference, not rvalue reference template <class t> const t& foo(t&& t) = delete; 

the question is: how can control kind of references generic template parameter t bind? , how scale multiple arguments (like in std::min case)?

given code, following fails compile

int = 0; foo(i);      // deleted function 

the reason forwarding reference overload selected because matching other 1 requires const qualification. if write

int const = 0; foo(i);      // fine 

in case overload taking lvalue reference selected.

thus, in order reject rvalues, deleted function needs take t const&& (this std::ref reject rvalues)

template <class t> const t& foo(const t& t) {   return t; }  template <class t> const t& foo(t const&& t) = delete; 

live demo


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -