Is there sense in using const std::string& arguments in C++17?How exactly is std::string_view faster than const std::string&?When would I pass const& std::string instead of std::string_view?When do you need a null terminated string in a read-only scenario?Why only string view?What are the differences between a pointer variable and a reference variable in C++?How to convert a std::string to const char* or char*?What is the difference between const int*, const int * const, and int const *?Why is “using namespace std;” considered bad practice?Static constant string (class member)Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsenum to string in modern C++11 / C++14 / C++17 and future C++20std::string_view compile time hashingWhat are the new features in C++17?Passing std::string_view to API execting const std::string&

How can medieval knights protect themselves against modern guns?

Public alias for non-public type

What ingredients can I add to no-knead bread to extend its life?

Bought a book that is in the public domain ... but the T&A of company says I can't redistribute it

Dollar cost averaging vs buy low/sell high

Is Fox News not classified as a news channel?

Do Klingons have escape pods?

Why would a berry have a slow-acting poison?

Simulate reproduction in a population of oozes

Starting with D&D: Starter Set vs Dungeon Master's Guide

Convention for inverted signal

Is there a word/phrase that can describe playing a musical instrument in a casual way?

What is the quickest way to raise Affection?

How to deal with an employee who is requesting a demotion?

My advisor wants me to make my PhD thesis weaker

What makes the planets to be symmetric

Exactly what color was the text on monochrome terminals with green-on-black and amber-on-black screens?

Why do some planes have flashing lights within the plane cabin?

Employer wants me to do something explicitly illegal

How to indicate "photograph by"

While I have six eyes, I don't need an optician

Can I call the airport to see if my boyfriend made it through customs?

Is there a sonic boom when flying nearby?

Why is SpaceX not also working on a smaller version of Starship?



Is there sense in using const std::string& arguments in C++17?


How exactly is std::string_view faster than const std::string&?When would I pass const& std::string instead of std::string_view?When do you need a null terminated string in a read-only scenario?Why only string view?What are the differences between a pointer variable and a reference variable in C++?How to convert a std::string to const char* or char*?What is the difference between const int*, const int * const, and int const *?Why is “using namespace std;” considered bad practice?Static constant string (class member)Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsenum to string in modern C++11 / C++14 / C++17 and future C++20std::string_view compile time hashingWhat are the new features in C++17?Passing std::string_view to API execting const std::string&






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









25


















By getting string_view in C++17 we got cheap method of passing both std::string and char* to functions that do not take ownership of the string and avoid making temporary copies. By using std::string passed by value and std::move we get explicit and fast passing of string ownership for both r-value and l-value references.



My question is: is there any benefit in using const std::string& as any function parameter in new C++ standard?










share|improve this question






















  • 12





    How exactly is std::string_view faster than const std::string&?, When would I pass const& std::string instead of std::string_view?, Why only string view?, provide various information about that topic. As your question is close to those, you should mention what particular information is missing in those question and their answers.

    – t.niese
    Sep 16 at 7:30







  • 4





    In one word: Null-termination.

    – Max Langhof
    Sep 16 at 7:37

















25


















By getting string_view in C++17 we got cheap method of passing both std::string and char* to functions that do not take ownership of the string and avoid making temporary copies. By using std::string passed by value and std::move we get explicit and fast passing of string ownership for both r-value and l-value references.



My question is: is there any benefit in using const std::string& as any function parameter in new C++ standard?










share|improve this question






















  • 12





    How exactly is std::string_view faster than const std::string&?, When would I pass const& std::string instead of std::string_view?, Why only string view?, provide various information about that topic. As your question is close to those, you should mention what particular information is missing in those question and their answers.

    – t.niese
    Sep 16 at 7:30







  • 4





    In one word: Null-termination.

    – Max Langhof
    Sep 16 at 7:37













25













25









25


3






By getting string_view in C++17 we got cheap method of passing both std::string and char* to functions that do not take ownership of the string and avoid making temporary copies. By using std::string passed by value and std::move we get explicit and fast passing of string ownership for both r-value and l-value references.



My question is: is there any benefit in using const std::string& as any function parameter in new C++ standard?










share|improve this question
















By getting string_view in C++17 we got cheap method of passing both std::string and char* to functions that do not take ownership of the string and avoid making temporary copies. By using std::string passed by value and std::move we get explicit and fast passing of string ownership for both r-value and l-value references.



My question is: is there any benefit in using const std::string& as any function parameter in new C++ standard?







c++ c++17






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 16 at 7:47









Wai Ha Lee

6,83513 gold badges43 silver badges68 bronze badges




6,83513 gold badges43 silver badges68 bronze badges










asked Sep 16 at 7:05









bartopbartop

5,73812 silver badges39 bronze badges




5,73812 silver badges39 bronze badges










  • 12





    How exactly is std::string_view faster than const std::string&?, When would I pass const& std::string instead of std::string_view?, Why only string view?, provide various information about that topic. As your question is close to those, you should mention what particular information is missing in those question and their answers.

    – t.niese
    Sep 16 at 7:30







  • 4





    In one word: Null-termination.

    – Max Langhof
    Sep 16 at 7:37












  • 12





    How exactly is std::string_view faster than const std::string&?, When would I pass const& std::string instead of std::string_view?, Why only string view?, provide various information about that topic. As your question is close to those, you should mention what particular information is missing in those question and their answers.

    – t.niese
    Sep 16 at 7:30







  • 4





    In one word: Null-termination.

    – Max Langhof
    Sep 16 at 7:37







12




12





How exactly is std::string_view faster than const std::string&?, When would I pass const& std::string instead of std::string_view?, Why only string view?, provide various information about that topic. As your question is close to those, you should mention what particular information is missing in those question and their answers.

– t.niese
Sep 16 at 7:30






How exactly is std::string_view faster than const std::string&?, When would I pass const& std::string instead of std::string_view?, Why only string view?, provide various information about that topic. As your question is close to those, you should mention what particular information is missing in those question and their answers.

– t.niese
Sep 16 at 7:30





4




4





In one word: Null-termination.

– Max Langhof
Sep 16 at 7:37





In one word: Null-termination.

– Max Langhof
Sep 16 at 7:37












1 Answer
1






active

oldest

votes


















28



















Yes.



The problem with std::string_view is that it doesn't remember if it points to a null-terminated string or not.



If you're writing a wrapper for a C api that uses null-terminated strings, you would have constantly copy your std::string_views into std::strings to make sure you have null-terminators.






share|improve this answer





















  • 2





    Exactly. Properly written C++17 code should provide overloads for const char *, const std::string & and std::string_view. Especially since many parts of standard library didn't receive string_view support (exceptions, for example).

    – StaceyGirl
    Sep 16 at 7:48







  • 17





    @StaceyGirl and here we go again with C++ complexity exploding in the face of the user

    – bartop
    Sep 16 at 7:50







  • 2





    Also, I'm pretty sure std::string_view is slower if one worries so much about micro-performance. Not like it matters, but it really ought to be slower. Passing a string const& will alias the string object which is a pointer in the worst case, and "no op" in the average case. Passing a string_view calls operator string_view to initialize a NRVO-copied string_view which initializes a pointer and a size. When given a const char* rather than a string it will call strlen, so...

    – Damon
    Sep 16 at 15:42






  • 3





    @StaceyGirl Does that mean that many parts of the C++17 standard library, such as exceptions, are not properly written C++17 code?

    – wrtlprnft
    Sep 16 at 19:35






  • 3





    @wrtlprnft Standard exceptions didn't receive string_view constructors and only have const char */const std::string & ones, so if you want to pass a string_view from a derived class, you will have to create an additional string copy - something that string_view was supposed to avoid while reducing number of overloads.

    – StaceyGirl
    Sep 16 at 19:40












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57951871%2fis-there-sense-in-using-const-stdstring-arguments-in-c17%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown


























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









28



















Yes.



The problem with std::string_view is that it doesn't remember if it points to a null-terminated string or not.



If you're writing a wrapper for a C api that uses null-terminated strings, you would have constantly copy your std::string_views into std::strings to make sure you have null-terminators.






share|improve this answer





















  • 2





    Exactly. Properly written C++17 code should provide overloads for const char *, const std::string & and std::string_view. Especially since many parts of standard library didn't receive string_view support (exceptions, for example).

    – StaceyGirl
    Sep 16 at 7:48







  • 17





    @StaceyGirl and here we go again with C++ complexity exploding in the face of the user

    – bartop
    Sep 16 at 7:50







  • 2





    Also, I'm pretty sure std::string_view is slower if one worries so much about micro-performance. Not like it matters, but it really ought to be slower. Passing a string const& will alias the string object which is a pointer in the worst case, and "no op" in the average case. Passing a string_view calls operator string_view to initialize a NRVO-copied string_view which initializes a pointer and a size. When given a const char* rather than a string it will call strlen, so...

    – Damon
    Sep 16 at 15:42






  • 3





    @StaceyGirl Does that mean that many parts of the C++17 standard library, such as exceptions, are not properly written C++17 code?

    – wrtlprnft
    Sep 16 at 19:35






  • 3





    @wrtlprnft Standard exceptions didn't receive string_view constructors and only have const char */const std::string & ones, so if you want to pass a string_view from a derived class, you will have to create an additional string copy - something that string_view was supposed to avoid while reducing number of overloads.

    – StaceyGirl
    Sep 16 at 19:40















28



















Yes.



The problem with std::string_view is that it doesn't remember if it points to a null-terminated string or not.



If you're writing a wrapper for a C api that uses null-terminated strings, you would have constantly copy your std::string_views into std::strings to make sure you have null-terminators.






share|improve this answer





















  • 2





    Exactly. Properly written C++17 code should provide overloads for const char *, const std::string & and std::string_view. Especially since many parts of standard library didn't receive string_view support (exceptions, for example).

    – StaceyGirl
    Sep 16 at 7:48







  • 17





    @StaceyGirl and here we go again with C++ complexity exploding in the face of the user

    – bartop
    Sep 16 at 7:50







  • 2





    Also, I'm pretty sure std::string_view is slower if one worries so much about micro-performance. Not like it matters, but it really ought to be slower. Passing a string const& will alias the string object which is a pointer in the worst case, and "no op" in the average case. Passing a string_view calls operator string_view to initialize a NRVO-copied string_view which initializes a pointer and a size. When given a const char* rather than a string it will call strlen, so...

    – Damon
    Sep 16 at 15:42






  • 3





    @StaceyGirl Does that mean that many parts of the C++17 standard library, such as exceptions, are not properly written C++17 code?

    – wrtlprnft
    Sep 16 at 19:35






  • 3





    @wrtlprnft Standard exceptions didn't receive string_view constructors and only have const char */const std::string & ones, so if you want to pass a string_view from a derived class, you will have to create an additional string copy - something that string_view was supposed to avoid while reducing number of overloads.

    – StaceyGirl
    Sep 16 at 19:40













28















28











28









Yes.



The problem with std::string_view is that it doesn't remember if it points to a null-terminated string or not.



If you're writing a wrapper for a C api that uses null-terminated strings, you would have constantly copy your std::string_views into std::strings to make sure you have null-terminators.






share|improve this answer














Yes.



The problem with std::string_view is that it doesn't remember if it points to a null-terminated string or not.



If you're writing a wrapper for a C api that uses null-terminated strings, you would have constantly copy your std::string_views into std::strings to make sure you have null-terminators.







share|improve this answer













share|improve this answer




share|improve this answer










answered Sep 16 at 7:43









HolyBlackCatHolyBlackCat

25.3k4 gold badges45 silver badges79 bronze badges




25.3k4 gold badges45 silver badges79 bronze badges










  • 2





    Exactly. Properly written C++17 code should provide overloads for const char *, const std::string & and std::string_view. Especially since many parts of standard library didn't receive string_view support (exceptions, for example).

    – StaceyGirl
    Sep 16 at 7:48







  • 17





    @StaceyGirl and here we go again with C++ complexity exploding in the face of the user

    – bartop
    Sep 16 at 7:50







  • 2





    Also, I'm pretty sure std::string_view is slower if one worries so much about micro-performance. Not like it matters, but it really ought to be slower. Passing a string const& will alias the string object which is a pointer in the worst case, and "no op" in the average case. Passing a string_view calls operator string_view to initialize a NRVO-copied string_view which initializes a pointer and a size. When given a const char* rather than a string it will call strlen, so...

    – Damon
    Sep 16 at 15:42






  • 3





    @StaceyGirl Does that mean that many parts of the C++17 standard library, such as exceptions, are not properly written C++17 code?

    – wrtlprnft
    Sep 16 at 19:35






  • 3





    @wrtlprnft Standard exceptions didn't receive string_view constructors and only have const char */const std::string & ones, so if you want to pass a string_view from a derived class, you will have to create an additional string copy - something that string_view was supposed to avoid while reducing number of overloads.

    – StaceyGirl
    Sep 16 at 19:40












  • 2





    Exactly. Properly written C++17 code should provide overloads for const char *, const std::string & and std::string_view. Especially since many parts of standard library didn't receive string_view support (exceptions, for example).

    – StaceyGirl
    Sep 16 at 7:48







  • 17





    @StaceyGirl and here we go again with C++ complexity exploding in the face of the user

    – bartop
    Sep 16 at 7:50







  • 2





    Also, I'm pretty sure std::string_view is slower if one worries so much about micro-performance. Not like it matters, but it really ought to be slower. Passing a string const& will alias the string object which is a pointer in the worst case, and "no op" in the average case. Passing a string_view calls operator string_view to initialize a NRVO-copied string_view which initializes a pointer and a size. When given a const char* rather than a string it will call strlen, so...

    – Damon
    Sep 16 at 15:42






  • 3





    @StaceyGirl Does that mean that many parts of the C++17 standard library, such as exceptions, are not properly written C++17 code?

    – wrtlprnft
    Sep 16 at 19:35






  • 3





    @wrtlprnft Standard exceptions didn't receive string_view constructors and only have const char */const std::string & ones, so if you want to pass a string_view from a derived class, you will have to create an additional string copy - something that string_view was supposed to avoid while reducing number of overloads.

    – StaceyGirl
    Sep 16 at 19:40







2




2





Exactly. Properly written C++17 code should provide overloads for const char *, const std::string & and std::string_view. Especially since many parts of standard library didn't receive string_view support (exceptions, for example).

– StaceyGirl
Sep 16 at 7:48






Exactly. Properly written C++17 code should provide overloads for const char *, const std::string & and std::string_view. Especially since many parts of standard library didn't receive string_view support (exceptions, for example).

– StaceyGirl
Sep 16 at 7:48





17




17





@StaceyGirl and here we go again with C++ complexity exploding in the face of the user

– bartop
Sep 16 at 7:50






@StaceyGirl and here we go again with C++ complexity exploding in the face of the user

– bartop
Sep 16 at 7:50





2




2





Also, I'm pretty sure std::string_view is slower if one worries so much about micro-performance. Not like it matters, but it really ought to be slower. Passing a string const& will alias the string object which is a pointer in the worst case, and "no op" in the average case. Passing a string_view calls operator string_view to initialize a NRVO-copied string_view which initializes a pointer and a size. When given a const char* rather than a string it will call strlen, so...

– Damon
Sep 16 at 15:42





Also, I'm pretty sure std::string_view is slower if one worries so much about micro-performance. Not like it matters, but it really ought to be slower. Passing a string const& will alias the string object which is a pointer in the worst case, and "no op" in the average case. Passing a string_view calls operator string_view to initialize a NRVO-copied string_view which initializes a pointer and a size. When given a const char* rather than a string it will call strlen, so...

– Damon
Sep 16 at 15:42




3




3





@StaceyGirl Does that mean that many parts of the C++17 standard library, such as exceptions, are not properly written C++17 code?

– wrtlprnft
Sep 16 at 19:35





@StaceyGirl Does that mean that many parts of the C++17 standard library, such as exceptions, are not properly written C++17 code?

– wrtlprnft
Sep 16 at 19:35




3




3





@wrtlprnft Standard exceptions didn't receive string_view constructors and only have const char */const std::string & ones, so if you want to pass a string_view from a derived class, you will have to create an additional string copy - something that string_view was supposed to avoid while reducing number of overloads.

– StaceyGirl
Sep 16 at 19:40





@wrtlprnft Standard exceptions didn't receive string_view constructors and only have const char */const std::string & ones, so if you want to pass a string_view from a derived class, you will have to create an additional string copy - something that string_view was supposed to avoid while reducing number of overloads.

– StaceyGirl
Sep 16 at 19:40




















draft saved

draft discarded















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57951871%2fis-there-sense-in-using-const-stdstring-arguments-in-c17%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown









Popular posts from this blog

Tamil (spriik) Luke uk diar | Nawigatjuun

Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

Training a classifier when some of the features are unknownWhy does Gradient Boosting regression predict negative values when there are no negative y-values in my training set?How to improve an existing (trained) classifier?What is effect when I set up some self defined predisctor variables?Why Matlab neural network classification returns decimal values on prediction dataset?Fitting and transforming text data in training, testing, and validation setsHow to quantify the performance of the classifier (multi-class SVM) using the test data?How do I control for some patients providing multiple samples in my training data?Training and Test setTraining a convolutional neural network for image denoising in MatlabShouldn't an autoencoder with #(neurons in hidden layer) = #(neurons in input layer) be “perfect”?