How to resolve ambiguous function selection in C++? [duplicate] - c++

This question already has answers here:
Overload resolution between object, rvalue reference, const reference
(1 answer)
C++ function overloading resolution involving pass-by-value, reference and constant reference
(3 answers)
Function Overloading Based on Value vs. Const Reference
(6 answers)
Closed 2 years ago.
How could i select the overloaded function that i mean to call ?
Consider this code:
void foo (std::vector<int> const &variable);
void foo (std::vector<int> variable);
For example in above code i want to call void foo (std::vector<int>);, I tried :
void bar ()
{
std::vector<int> tmp;
foo(tmp);
foo(static_cast<std::vector<int>(tmp));
foo(boost::implicit_cast<std::vector<int>>(tmp));
foo((std::vector<int>)tmp);
foo(std::vector<int>(tmp));
}
But it's failed, How could i do that without changing the functions signature ?

Related

Use of const and & in functions C++ [duplicate]

This question already has answers here:
C++ Return value, reference, const reference
(5 answers)
What is a reference variable in C++?
(12 answers)
Closed 1 year ago.
I am trying to understand the useage of 'const' and '&' in the following function declaration. I know that the last 'const' means the function cannot change member variables in the class and that 'const std::string& message' means the variable passed to the function cannot be changed, but I don't understand the meaning of 'const Logger&'. What is the purpose of this first 'const' and why is there an '&' following 'Logger'? Is this function meant to return an address or a pointer?
const Logger& log(const std::string& message) const;
So const Logger& is the return type of log. const means you will not be able to edit the return value at all. The return type Logger& means you'll get a reference to a Logger and not a copy of it.

Why is this variable considered an lvalue? [duplicate]

This question already has answers here:
Why are rvalues references variables not rvalue?
(3 answers)
Rvalue Reference is Treated as an Lvalue?
(4 answers)
Closed 3 years ago.
Considering this code
class T {
public:
T(T& x) = delete;
T(T&& x) {}
};
void foo(T&& b) {
T y(b);
}
I was expecting that b; which is an rvalue by declaration; and seemingly usage, should be passed into the move constructor of T in foo().
Instead; I get a compilation error reporting that T& has been deleted.
Replacing it with
void foo(T&& c) {
T y(std::move(c));
}
Results in the expected success; but obviously one doesn't want to litter their code with std::move everywhere.
As tempting as it is to blame visual studio - in this case I suspect it's my understanding that's wrong. Can someone please explain why move constructor isn't used?

using operator & after class method name [duplicate]

This question already has answers here:
const& , & and && specifiers for member functions in C++
(2 answers)
Closed 4 years ago.
I found the C++ class with API like this:
class foo
{
.....
public:
int& func1() & ;
int func1() && ;
.....
}
what does operator & and && do after the method name and what is the difference between these two function.
These are called "ref qualifiers" and allow you to overload a member function depending on the value category of *this.
int& func1() & means: this overload of func1 can be invoked on any instance of *this which can be bound to an lvalue reference.
int func1() && means: this overload of func1 can be invoked on any instance of *this which can be bound to an rvalue reference.
E.g.
foo f; f.func1(); // calls &-qualified version
foo{}.func1(); // calls &&-qualified version

Function Overloading with const [duplicate]

This question already has answers here:
Why is this call to member function ambiguous?
(3 answers)
Closed 5 years ago.
struct A
{
void fn(double a) const {}
void fn(int a){}
};
int main()
{
A().fn(1.);
}
For the above mentioned function why does the compiler produce an ambiguity; Both the types are different.
Why would you like to pass an int only to a non-const A?
There are two parameters to each member function, this and a. So you require a const A* for this and doublefor a, or non-const A* and int.
And the call doesn't fully match either alternative, as you have non-const Aand double. So the compiler can either convert A() to const A, or doubleto int. And it cannot decide which is the best.

Reference as last symbol in C++ function declaration? [duplicate]

This question already has answers here:
What does the single ampersand after the parameter list of a member function declaration mean?
(3 answers)
What is "rvalue reference for *this"?
(3 answers)
Closed 6 years ago.
Looking at Boost::Optional optional class template header I come across this:
T const& operator*() const&
T& operator*() &;
T&& operator*() &&;
For the life of me I can't find this syntax anywhere else (a reference as the last symbol) I would assume it has something to do with overloading on the type(const l-val, l-val, r-val) of the object the operator belongs to, but I haven't seen this described anywhere.
Could someone tell me what this syntax means?