简体   繁体   中英

Passing string literal to function which expect string

Let us suppose i have a function which expect argument to be a string(non const). But As we know that string literals are const in nature. Is there is any way by which i can pass string literal to a function which is expecting non const. i know it will cause error as literals are const in nature but function is expecting non const. Any way to way to cast this that function can work. Heard that there is const_cast. Can i use it?

   void str(string &s); // my function which is expecting a non const string
   // main . i want to do something like this 
   str("Blah"); // error

  void str(string s) // it works fine ? why ?

When your function is declared as

void str(string &s);

It can only be called with an expression that can be converted to a string& .

If you try calling the function with

str("Blah");

it does not work because the string literal "Blah" cannot be converted to a std::string& . The temporary std::string object that the compiler is able to construct from the string literal can be converted to a std::string const& or a std::string but not a std::string& .

What you've asked about in the updated question (passing a reference to a non-const std::string object) won't (or at least shouldn't) work.

It's absolutely true that the compiler can create a temporary string object from a string literal. This temporary object can be passed by value. You can also bind a reference to a const string object to a temporary string object (and more generally, a reference to const T to a temporary T).

But, a reference to a non-const type can't be bound to a temporary object, so the compiler will (or at least should) reject your code.

There is one exception though: Microsoft's older compilers did allow a non-const reference to bind to a temporary object. As some of Microsoft's people have pointed out, this is fairly harmless--it allows you to modify the temporary object, which is generally fairly useless (the object will simply be destroyed when the function returns, so the nothing else can/will ever see the modifications, as you'd normally expect to happen when modifying something passed by non-const reference. On the other hand, they argue that since nothing else can ever see the modifications, allowing them is fairly harmless. I tend to disagree with the latter, as allowing something to happen that's almost certainly happened by accident is harmful not because of what happens in the program afterwards, but simply because the compiler has failed to warn the programmer about having done something that's almost certainly a mistake.

I'm not sure exactly when Microsoft fixed this shortcoming in their compiler, but recent versions (at least since VS 2015) reject the code as they should. I know they accepted it at one time, but I'm not sure about the exact version when it changed.

Summary

Code that attempts to bind a non-const reference to a temporary should be rejected, but with Microsoft's compiler it won't necessarily be.

You cannot pass a temporary to a function which accepts a non-const reference. The temporary dies and you have a dangling reference, if it even compiles.

However, if you use a const reference, the lifetime of the temporary will be prolonged until the end of the function.

It's absolutely fine. std::string has a ctor that looks like:

string(const char* text);

So the call to str("Blah") will create a temporary string object (by calling the above ctor), and then the str method will be called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM