Skip to main content
7 votes
1 answer
463 views

Is const T& effectively the same as T&& with std::forward for rvalue argument when be passed into another function?

Say I have a const T& version: template <typename T> void yetAnotherFunc( const T &input ) { // do something... } template <typename T> void myFunc( const T &input ) { ...
PkDrew's user avatar
  • 2,659
0 votes
1 answer
99 views

Why const reference is called and not rvalue reference and how std::move works? [duplicate]

#include <iostream> void bar(std::string& s) { std::cout << "ref" << std::endl;} void bar(const std::string& s) { std::cout << "const ref" << ...
Chp Cht's user avatar
3 votes
1 answer
891 views

How to avoid clang-tidy warning when passing shared_ptr to thread function?

I have C++ code that looks (simplified) like this: #include <unistd.h> #include <iostream> #include <memory> #include <string> #include <thread> std::thread the_thread; ...
oliver's user avatar
  • 6,881
0 votes
2 answers
289 views

Weird behavior with std::string reference class member

Given this code: #include <iostream> class Foo { public: Foo(const std::string& label) : label_(label) {} void print() { std::cout << label_; }...
morpheus's user avatar
  • 20.7k
2 votes
2 answers
256 views

Why does the "in" keyword allow mutation of properties C#?

I am currently working on a function where an object (of self written type Catalog) is passed and shall not be mutated / changed in the called function. Here a quick example. public override bool ...
Patrick Cerny's user avatar
2 votes
2 answers
187 views

C++ struct as function argument vs. multiple const ref parameters vs. C++ core guidelines vs. performance?

I'm currently trying to decide whether to "structify" a rather long parameter set: void fooCopy1(std::string const& source, std::string const& destination, std::string const& ...
Martin Ba's user avatar
  • 39.3k
0 votes
1 answer
39 views

Is relying on the const-ness of arguments bind to const& parameters a recipe for thread un-safety?

I was just saw code like this /* whatever */ foo(std::string const& s) { // stuff auto L = s.length(); int i{/* init based on L */}; while (i < L) { // do other stuff and maybe +...
Enlico's user avatar
  • 30.1k
0 votes
3 answers
1k views

Multiple functions with the same name but their parameters are either constant or received by value or by reference

The title is a bit lengthy, but it's best explained by an example: Suppose we have the following functions in C++: void SomeFunction(int num) { //1 } void SomeFunction(int& num) { //2 } void ...
DannyBoy's user avatar
-1 votes
2 answers
358 views

Is there any realistic use case for passing pointers by constant reference rather than by value?

Since a reference (some-type&) behaves as a constant pointer (some-type * const), passing an argument via a "constant reference to pointer" parameter (some-type * const&) seems a bit ...
Enlico's user avatar
  • 30.1k
1 vote
1 answer
153 views

(C++) Using multiple operator overloads with const reference parameters [duplicate]

I have been working on a matrix class and I have recently learnt about passing const references to operator overloads so that I can have multiple of them on the same line. The problem I encountered is ...
AssistantToTheRegionalManager's user avatar
2 votes
1 answer
219 views

C++20 : Memory allocation of literal initialization of const references

I am trying to optimize for speed of execution a piece of code using the factory design pattern. The factory will produce many objects of a class having some members that are constant throughtout the ...
LastStarDust's user avatar
0 votes
0 answers
33 views

Are temporaries const? Pass by const reference [duplicate]

#include <iostream> using namespace std; void f(const int& x) { } int g() { int x = 0; return x; } int main() { f(g()); } If i remove the const from f it doesnt work; What ...
ionescu radu's user avatar
2 votes
2 answers
1k views

Pass-by-value and std::move vs forwarding reference

I encounter the pass by value and move idiom quite often: struct Test { Test(std::string str_) : str{std::move(str_)} {} std::string str; }; But it seems to me that passing by either const ...
Alex O's user avatar
  • 1,976
0 votes
1 answer
643 views

Using find_if with a vector of pointers: How to pass pointer by const reference to lambda?

In the following code I try to compare a vector of pointers via find_if and determine which contains a member a == 5 (in this case both of course, but it shows my case). However it doesn't compile. #...
glades's user avatar
  • 5,344
3 votes
1 answer
444 views

Why does C++ give preference to rvalue reference over const reference while function call? [duplicate]

So I wrote a code in C++ 11 #include <iostream> using namespace std; void print (int &&a) { cout<<"rval ref"; } void print (const int& a) { cout<<&...
simplekind's user avatar

15 30 50 per page