Sujet : Re: Counting Characters!!
De : Bonita.Montero (at) *nospam* gmail.com (Bonita Montero)
Groupes : comp.lang.c++ alt.comp.lang.c++.miscDate : 17. Sep 2024, 12:41:06
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vcbpsf$3gcro$1@raubtier-asyl.eternal-september.org>
References : 1 2 3 4 5 6
User-Agent : Mozilla Thunderbird
Am 17.09.2024 um 09:21 schrieb
Muttley@dastardlyhq.com:
On Mon, 16 Sep 2024 19:08:14 +0200
Bonita Montero <Bonita.Montero@gmail.com> boringly babbled:
Am 16.09.2024 um 18:08 schrieb Muttley@dastardlyhq.com:
On Mon, 16 Sep 2024 17:55:02 +0200
Bonita Montero <Bonita.Montero@gmail.com> boringly babbled:
Am 14.09.2024 um 21:41 schrieb Chris Ahlstrom:
DMZ wrote this copyrighted missive and expects royalties:
>
This simple program will count characters for you!!
>
#include <stdio.h>
>
int main()
{
printf(" = %d characters", printf("Hello World!"));
>
return 0;
}
>
Cute.
>
>
Nice, but this counts the characters at compile-time:
>
std::string_view sv( "Hello World!" );
std::cout << sv << " = " << sv.length() << " characters" << endl;
>
So does sizeof("Hello world!");
>
>
But sizeof(string_literal) includes the null-terminating character.
Gosh! Maybe you could do sizeof()-1 then. Just a thought!
How about this:
template<size_t N>
consteval size_t xstrlen( char const (&x)[N] )
{
return N - !x[N - 1];
}