Question Details

No question body available.

Tags

c++

Answers (1)

Accepted Answer Available
Accepted Answer
April 18, 2025 Score: 9 Rep: 36,893 Quality: Expert Completeness: 80%

No, it is not redundant.

You should definitely use std::ref / std::cref if you want to pass an argument by reference to a thread (and keep reference semantics).

This is because the arguments for a thread are always copied (or moved if applicable) when the thread is created.

Therefore, a "normal" reference will not preserve reference semantics, but std::ref/std::cref return a std::referencewrapper which will preserve it:

std::referencewrapper is a class template that wraps a reference in a copyable, assignable object.

Your code might seem to work as you expect, but in fact your std::string argument is being copied in the second case (without std::cref) - and I assume this is not what you intended.

This is demonstrated by adding some prints to your code:

#include 
#include 
#include 

bool thread
func(int asyncId, const std::string& str) { std::cout