badri
badri

Reputation: 627

Is there any reason to change from using const char* to string in this case below

I understand that it is always advisable to use std::string compared to const char * because of the inadvertent changes that may happen because of using a pointer. But for simple usages where there can't be any inadvertent changes by any other engineer, is it still a big deal. For e.g. see the code below -

So in this example below, I could have used std::string BothUserNameAndPassword = "abracadabra";

I see time to time having to deal with code reviews that has code such as the above.

#include<iostream>
using namespace std;
void authenticatePassword(std::string const & username, std::string 
const &password);
int main()
{
        const char* BothUserNameAndPassword = "abracadabra";        
        authenticatePassword(BothUserNameAndPassword, BothUserNameAndPassword);
}

void authenticatePassword(std::string const & username, std::string const &password)
{
        cout << "username = "<<username<<" and password = "<<password <<endl;
}

Upvotes: 0

Views: 515

Answers (2)

lubgr
lubgr

Reputation: 38287

Is there any reason to change from using const char* to string [...]?

Yes, there is. The function signature reveals that two const std::string& arguments are expected. Now what happens if you pass in a const char* object? A temporary std::string instance is implicitly constructed. By going with

const std::string BothUserNameAndPassword = "abracadabra";
authenticatePassword(BothUserNameAndPassword, BothUserNameAndPassword);

you don't make this extra effort. I wouldn't argue that this is a performance-critical tweak, but it might be considered more readable, as you don't have to think about conversions when having a look at the function signature and the variable you are passing to it.

Note that from C++17 on, you might also want to consider std::string_view for such cases (both function arguments and binding the literal to a variable).

Upvotes: 3

Matthieu Brucher
Matthieu Brucher

Reputation: 22023

Your user will probably specify the username and password externally, which will be read in a string. So no reason not to use a string from the start. The double string creation artifact of this example may not be a real world or production case (at least I hope it's not...).

In modern C++ (post C++17), there is no good reason to use const char* in new code anymore, you should use string_view instead. This efficiently replaces the pointer as it has the size of the string and can also efficiently slice bits of the string. It is NOT suited for something that will end up being stored in a string. In that case, you should use a string from the start.

So there are two use cases, one fitted for string and one for string_view:

  • storing the data
  • using the data

In this specific case, I would do:

int main()
{
    constexpr std::string_view BothUserNameAndPassword = "abracadabra";
    authenticatePassword(BothUserNameAndPassword, BothUserNameAndPassword);
}

void authenticatePassword(std::string_view username, std::string_view password)
{
        cout << "username = "<<username<<" and password = "<<password <<endl;
}

Upvotes: 3

Related Questions