Reputation: 17051
I have a method:
void Foo::Bar(const std::string &str)
{
printf("%d", str.length());
}
and it works seamlessly when I do
foo.Bar("hello");
I thought "hello"
was a const char *
, not a std::string
?
Upvotes: 1
Views: 2101
Reputation: 385194
Your string literal is a char const[6]
.
The name of it decays to const char*
for passing to the function.
Then an implicit conversion is performed, because:
rvalue
can be bound to itconst char*
can be implicitly converted into an std::string
rvalue
.Upvotes: 0
Reputation: 147
somewhat related to your question: if there is a time in C++ where you *explicit*ly don't want the compiler to change types for you like this, when a compatible constructor exists, you use the keyword "explicit" before the constructor. Then it will give you a compiler error. You can only do this with the types you create, however. Not with STL types like string.
Upvotes: 2
Reputation: 95509
There are two ways user-defined types can be implicitly converted:
Any constructor that takes a single parameter and does not use the keyword "explicit" defines an implicit conversion (from the type of the parameter, to the type of the object being constructed). The standard string class intentionally does not use "explicit" in order to provide the convenience of this conversion.
Upvotes: 9
Reputation: 57708
Because there is an ?implicit? conversion from char *
to std::string
using the std::string(char *)
constructor.
Upvotes: 1
Reputation: 16197
Technically it's a char const[6]
but yes you should be able to do this thanks to implicit conversion and one of std::string
constructors.
Upvotes: 6
Reputation: 308206
Because std::string has a constructor that takes a const char *
, C++ will automatically construct a temporary string object for you.
Upvotes: 8