C++ Logo

std-discussion

Advanced search

Re: Inconsistency constructing std::string from C-string vs. from another std::string

From: Thiago Macieira <thiago_at_[hidden]>
Date: Mon, 18 May 2020 18:47:05 -0700
On Monday, 18 May 2020 18:34:08 PDT Brian Liu via Std-Discussion wrote:
> *EDIT*: This question isn't about which default constructor is called, or
> whether they do different things. They *do* different things, and different
> constructors *are* called. *This question is about why they don't do the
> same thing.* In other words, this is more of a philosophical question than
> a technical question.

Because they designed that way. You don't need an offset when passing a CharT
pointer, since you can just add to the pointer: But when creating a substring
from an existing string, you want to pass the string intact so the length can
be obtained from it.

Leaving the length as a defaulted parameter is more convenient. Passing
std::string::npos requires more typing if you had to type it, whereas passing
"start from the beginning" is just a zero:

    std::cout << std::string(std, 0, 4) << std::endl;

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel System Software Products

Received on 2020-05-18 20:50:14