All Questions
382
questions
1
vote
1
answer
70
views
Why can't pass object reference to a template?
Here's my code:
#include <iostream>
#include <vector>
struct LogParamScaler
{
};
template <LogParamScaler &T>
struct QRange {
};
int main()
{
LogParamScaler ...
1
vote
1
answer
257
views
C++11 recursive function using "&&" rvalue reference leads to compilation error
I tried to write some algorithm using recursive function and met this:
#include<vector>
using namespace std;
void f(vector<int>&& v) {
if (!v.empty()) {
// do some work
...
0
votes
1
answer
503
views
Lifetime extension of temporary objects: what is the full expression containing a function call?
Introduction
Say there is a Container class which stores Widget objects.
There is an iterator class in charge of navigating such container. This iterator class (MyIterator) takes a const-reference to ...
-3
votes
2
answers
518
views
Printing variable value using Reference to const gives different result
I am learning about references in C++. So i am trying out different examples to better understand the concept. One example that i could not understand is given below:
double val = 4.55;
const int &...
0
votes
1
answer
452
views
What is the significance of int &x declaration in class definition in C++ [duplicate]
class Pointer {
private:
int &x;
public:
Pointer(int &y) : x(y) {}
int getT() { return x; }
};
int main()
{
int w = 40;
Pointer test(w);
std::cout << &test &...
0
votes
1
answer
104
views
Need I really use another template in this case
template<class Key, class Value>
class ThreadSafeMap
{
std::mutex m_;
std::map<Key, Value> c_;
public:
Value get(Key const& k) {
std::unique_lock<decltype(m_)&...
1
vote
1
answer
389
views
const reference parameter with default value
I have one class with a constructor that takes another struct as a parameter, and that has a default value. Something like this:
class A
{
public:
A(someStruct st = someStruct::defaultStruct());
};
I'...
7
votes
5
answers
663
views
Does each expression in C++ have a non-reference type
Hi i am reading about expression in C++ and across the statement
Statement 0.0
Each expression has some non-reference type
The quoted statement is from en.cppreference.com/w/cpp/language/...
6
votes
1
answer
689
views
Using declval with a reference type
I see some code examples where the type used to instantiate the std::declval template function is specified as a reference type rather than just a type, as in:
std::declval<T &>()
as ...
0
votes
0
answers
65
views
Returns as value or as reference through parameter?
std::vector<std::string> get() {
std::vector<string> result {};
for (int i = 0; i < 10000) {
result.emplace_back("test" + to_string(i));
}
return ...
0
votes
1
answer
57
views
Can smart pointer be safer by disallowing automatic variables?
Let's consider such example:
#include <memory>
int main() {
int x = 3;
std::shared_ptr<int> p{&x};
//std::shared_ptr<int> p = &x;
}
This program has a double-...
1
vote
1
answer
104
views
Does it make sense to move onto function reference param
void foo(const std::string& str);
foo(std::move(localstr));
If I know that I won't use this string anymore, will I have any gain by moving it if the function expects reference?
In general, there ...
0
votes
1
answer
218
views
deduced conflicting types for parameter '_BIter' ('int*' and 'int')
I was trying to implement a simple reverse array function:
void reverse(int (&arr)[], int n)
{
int start = 0, end = n - 1;
while (start <= end)
{
int temp = arr[start];
arr[start]...
0
votes
2
answers
63
views
A list iterator reference - Program output
Given the following algorithm:
#include <iostream>
#include <list>
int main ()
{
std::list<int> mylist = {5,10,15,20};
std::cout << "mylist contains:"<<&...
0
votes
1
answer
767
views
Binding a const reference to a literal in C++ [duplicate]
Hi i am reading about auto and const in C++ and came to know about 2 examples. The 2 examples are as follows:
auto &h = 42;
const auto &j = 42;
Now my questions are as follows:
Why in the ...