Questions tagged [const-reference]

131 questions
3
votes
2 answers

C++ When to use const Reference over Forwarding Reference

Consider we need to implement a function f with a templated argument T t. The function should not copy t and accept both rvalues and lvalues, therefore two implementations are possible: template void f(const T& t) { ... } template
Seriously
  • 884
  • 1
  • 11
  • 25
3
votes
1 answer

Why does my operator=(T&&) template only bind to a const& but not a &&?

When writing an overloaded function from an rvalue reference and a const reference, you might have code duplication, so I sometimes do both with the same code. As shown here: #include #include struct A { template…
bitmask
  • 32,434
  • 14
  • 99
  • 159
3
votes
1 answer

Can lifetime of objects in a initializer list be extended?

I have the impression that std::initializer_list might behave like literal strings in C++, and even further they might extend the lifetime of const references. Is that a correct assessment? Can objects in an initializer_list be somehow referenced…
alfC
  • 14,261
  • 4
  • 67
  • 118
3
votes
1 answer

Is this valid C++ code according to standard?

I have this sample code: struct A { bool test() const { return false; } }; template class Test { public: Test(const T& t = T()) : t_(t){} void f() { if(t_.test()) { //Do…
Naveen
  • 74,600
  • 47
  • 176
  • 233
3
votes
2 answers

function call ambiguity with pointer, reference and constant reference parameter

What I am trying to do is, allow a pointer, reference or constant reference to be passed with the setter function: class A{ std::string * p; std::string st; public: A():p(0) {} A& setS(const std::string& s){ …
Jahid
  • 21,542
  • 10
  • 90
  • 108
3
votes
1 answer

Return local variable to const ref from lambda

const TBigType& a = [](){ TBigType result; // ... return result; }(); use(a); // by const ref Is it ok to capture result in const ref like this?
vladon
  • 8,158
  • 2
  • 47
  • 91
3
votes
1 answer

Assignment to const reference in Visual C++ 2013

In Visual C++ 2013, it is legal to assign a temporary value to a const reference, for example const int& a = 1; In a large software project, I now stumbled upon a single line of code that took around 10 ms to execute: const std::vector& desc…
mOfl
  • 478
  • 3
  • 14
3
votes
2 answers

Is it possible to change the temporary object and to pass it as an argument?

Is it possible to change the temporary object and to pass it as an argument? struct Foo { Foo& ref() { return *this; } Foo& operator--() { /*do something*/; return *this; } // another members }; Foo getfoo() { return Foo(); } // return…
Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88
2
votes
0 answers

Lifetime extension of default initialized const ref& in an aggregate

I know that binding prvalues to const ref variables will extend the lifetime of the temporary. Now I thought I'd be clever to have a struct options that contains a const ref field B_options (which is used for initializing a subclass in my context).…
2
votes
1 answer

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 execution of the program, and some members that…
2
votes
2 answers

Understanding const reference in assignment when constructing object

Today I see this piece of code and I'm wondering to know what it is exactly doing this const reference in an assignment where a new object is created. (I don't know how to name this kind of assignments.) std::string const& p = s.c_str(); // s is a…
cbuchart
  • 10,847
  • 9
  • 53
  • 93
2
votes
1 answer

Difference between ordinary parameter, reference parameter and const reference parameter passed by ordinary object and object created temporary

Since I'm a beginner in c++, some questions don't quite understand. This question came across by accident while I was reading C++ primer 5th. I have a Cat class with 3 different constructors(named by Constructor 1, Constructor 2, Constructor…
2
votes
3 answers

Why returning a const reference via delegation gets a segmentation fault in c++, while without the delegation just be "fine"

Consider #include #include const auto& foo() { return std::make_pair("hi there", 2020); } int main() { //const auto& p = std::make_pair("hi there", 2020); // Okay, just warning, no segfault const auto& p = foo();…
Mark Taylor
  • 117
  • 8
2
votes
2 answers

Template type deduction of const reference type

It is a bit confusing to me about how C++ 11 does template deduction when const references to a template parameter types are used. Consider the following program: template void test_func(const T &a){ (*a)++; } int main() { //…
Marcus_Ma
  • 155
  • 1
  • 1
  • 6
2
votes
3 answers

Why the need for both const reference and const member function?

I'm doing the exercises from "Programming Principles and Practice using C++" and I found a class with this member function here: const vector &get_name() const { return name; } where name is a vector: vector< string> name; The book…
Theodore
  • 179
  • 12
1 2 3
8 9