This question already has answers here:
Why {} is better candidate to be int than string for C++ overload resolution? [duplicate]
(1 answer)
Overload resolution with an empty brace initializer: pointer or reference? [duplicate]
(1 answer)
Closed 4 months ago.
In this code:
#include <string>
#include <iostream>
void my_function(const char* value)
{
std::cout << "char * " << (value ? value : "nullptr");
}
void my_function(const std::string &value)
{
std::cout << "string";
}
int main()
{
my_function({});
}
https://godbolt.org/z/56bxzjM1M
The const char * overload is chosen, passing a nullptr pointer. Can somebody explain why? I assume this is something weird with initializer lists? (By the way, this happens with my_function(string value) too, so it isn't the reference).
This caused a crash in our code because somebody added a const char * overload, causing the rare caller who passed {} to send in a nullptr.
Related
This question already has answers here:
Overload resolution: assignment of empty braces
(2 answers)
Closed 10 months ago.
Overload resolution favours to consider {} as being of some fundamental type as opposed to some container.
For example:
#include <iostream>
#include <string>
void foo(const std::string&) {std::cout << "string\n";}
void foo(int) {std::cout << "int\n";}
int main() { foo({}); }
That compiles without any diagnostics and outputs:
int
https://godbolt.org/z/zETfrs5as
If to comment out the int overload then it works fine with string.
The question is why? For programmer's standpoint it can be confusing
illusion.
From over.ics.list#9.2:
if the initializer list has no elements, the implicit conversion sequence is the identity conversion. [ Example:
void f(int);
f( { } ); // OK: identity conversion
— end example ]
Thus, the conversion from {} to int is an identity conversion, while {} to const std::string& is a user-defined conversion. And since the identity conversion is a better match, the overload corresponding to int will be chosen.
This question already has answers here:
Meaning of 'const' last in a function declaration of a class?
(12 answers)
Closed last year.
I'm learning C++ i saw a const after an operator function.
It doesn't make sense because the function returns the same value regardless of the const.
What's the purpose of using it?
using namespace std;
class Person {
public:
int age;
Person(int age) : age(age) {}
int operator *(int &b) const {
return b;
}
};
int main() {
Person *p = new Person(11);
int a = 19;
cout << *p * a; // prints 19
delete p;
}
A const operator behind a member function applies to the this pointer, i.e. it guarantees that the object you call this function on may not be changed by it. It's called a const-qualified member function
If you tried, in this example, to change the persons age in the openrator*(), you would get a compile error.
This question already has answers here:
non-const lvalue reference to type ... cannot bind to a temporary of type [duplicate]
(1 answer)
Non const lvalue references
(2 answers)
Since a string literal is considered an lvalue, why must the binding lvalue reference be const?
(3 answers)
Closed 2 years ago.
I have this code but compilation failed:
class TextBlock
{
public:
TextBlock(std::string &s)
{
text = s;
}
private:
std::string text;
};
int main()
{
TextBlock tb("Hello");
std::cout<< tb[0] << std::endl ;
}
why when the constructor is TextBlock(const std::string &s) the above code can compile success ?
the constructor is specting a ref to a string do instead:
std::string x{"hello"};
TextBlock tb(x);
This question already has answers here:
Is there any way in C++ to refer to a function template while neither calling it nor supplying its template parameters?
(4 answers)
Convert template function to generic lambda
(2 answers)
How do I make a functor out of an arbitrary function?
(2 answers)
Closed 2 years ago.
I have a function returning a std::pair, which I want to use to call an overloaded member function. I am not aware of any way to unpack a tuple in-place (which I'd prefer), so std::apply is my best bet, I think.
This works with a combination of tuple_cat and forward_as_tuple when the member function is not overloaded, but otherwise, the compiler complains about an unresolved overload.
That is understandable, given that std::apply just takes a function pointer, but I'd still like to know whether it can be made to work in a readable way.
Here's some example code:
#include <iostream>
#include <tuple>
#include <utility>
std::pair<int, int> func() { return { 0, 1 }; }
class MyClass {
public:
int memFun(int);
int memFun(int, int);
};
int MyClass::memFun(int i){ return 2*i; }
int MyClass::memFun(int a, int b){ return a+b; }
int main(){
MyClass myClass;
/* works */
std::pair pair { func() };
int number { myClass.memFun( pair.first, pair.second ) };
/* doesn't work (unresolved overload) */
int number { std::apply(
&MyClass::memFun,
std::tuple_cat( std::forward_as_tuple(myClass), func() )
) };
std::cout << number << "\n";
return 0;
}
Is there an elegant (=readable) way to do this without a temporary?
I only found this question and so far don't think it fully applies.
This question already has answers here:
"const T &arg" vs. "T arg"
(14 answers)
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 7 years ago.
// const objects
#include <iostream>
using namespace std;
class MyClass {
int x;
public:
MyClass(int val) : x(val) {}
const int& get() const {return x;}
};
void print (const MyClass& arg) { // Need to understand this line
cout << arg.get() << '\n';
}
int main() {
MyClass foo (10);
print(foo);
return 0;
}
I am new to C++. Need to understand what are the parameters passed in print function. If this is address then why are we passing foo is print function call.
The & in void print (const MyClass& arg) that arg is passed by reference. It is C++s way to make pointers and things a little bit easier.
References allow you to manipulate a variable inside of a function and make the results visible on the outside too. So a bit like pointers. But you don't need to explicitly get the address of the variable to do that.
The const statement is a way to prevent the described behavior. const forbids the manipulation of arg inside print.