Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Only God can make random selections.


devel / comp.lang.c++ / How to concat two constexpr c-string?

SubjectAuthor
* How to concat two constexpr c-string?wij
+* Re: How to concat two constexpr c-string?Öö Tiib
|+* Re: How to concat two constexpr c-string?wij
||`* Re: How to concat two constexpr c-string?Bonita Montero
|| `* Re: How to concat two constexpr c-string?wij
||  `- Re: How to concat two constexpr c-string?Bonita Montero
|`* Re: How to concat two constexpr c-string?JiiPee
| `- Re: How to concat two constexpr c-string?David Brown
+* Re: How to concat two constexpr c-string?Bonita Montero
|+- Re: How to concat two constexpr c-string?Bonita Montero
|`- Re: How to concat two constexpr c-string?JiiPee
`* Re: How to concat two constexpr c-string?Alf P. Steinbach
 `* Re: How to concat two constexpr c-string?JiiPee
  `- Re: How to concat two constexpr c-string?Alf P. Steinbach

1
How to concat two constexpr c-string?

<2eea619a-b7f4-4df1-9bdc-6b2582c03991n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1190&group=comp.lang.c%2B%2B#1190

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:ad4:55d0:0:b0:649:463d:bf40 with SMTP id bt16-20020ad455d0000000b00649463dbf40mr652734qvb.1.1693044100547;
Sat, 26 Aug 2023 03:01:40 -0700 (PDT)
X-Received: by 2002:a17:902:d508:b0:1c0:df9f:e488 with SMTP id
b8-20020a170902d50800b001c0df9fe488mr594823plg.12.1693044099919; Sat, 26 Aug
2023 03:01:39 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Sat, 26 Aug 2023 03:01:39 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=124.218.76.41; posting-account=0Ek0TQoAAAAS0oceh95IuNV59QuIWNeN
NNTP-Posting-Host: 124.218.76.41
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <2eea619a-b7f4-4df1-9bdc-6b2582c03991n@googlegroups.com>
Subject: How to concat two constexpr c-string?
From: wynii...@gmail.com (wij)
Injection-Date: Sat, 26 Aug 2023 10:01:40 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1140
 by: wij - Sat, 26 Aug 2023 10:01 UTC

constexpr const char K1[]="abc";
constexpr const char K2[]="efg";
constexpr const char K3[]= ??? // "abcefg" expected

I'd like K3 to be "abcefg", how should I do?

Re: How to concat two constexpr c-string?

<610a03f1-1ff9-4ef9-b2c5-8634f6cec8d3n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1191&group=comp.lang.c%2B%2B#1191

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:a05:620a:269f:b0:76e:f8bb:ff6 with SMTP id c31-20020a05620a269f00b0076ef8bb0ff6mr370211qkp.0.1693046674138;
Sat, 26 Aug 2023 03:44:34 -0700 (PDT)
X-Received: by 2002:a05:620a:4bc5:b0:76c:b6b3:a8d5 with SMTP id
sw5-20020a05620a4bc500b0076cb6b3a8d5mr468031qkn.2.1693046673981; Sat, 26 Aug
2023 03:44:33 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Sat, 26 Aug 2023 03:44:33 -0700 (PDT)
In-Reply-To: <2eea619a-b7f4-4df1-9bdc-6b2582c03991n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=145.14.19.203; posting-account=pysjKgkAAACLegAdYDFznkqjgx_7vlUK
NNTP-Posting-Host: 145.14.19.203
References: <2eea619a-b7f4-4df1-9bdc-6b2582c03991n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <610a03f1-1ff9-4ef9-b2c5-8634f6cec8d3n@googlegroups.com>
Subject: Re: How to concat two constexpr c-string?
From: oot...@hot.ee (Öö Tiib)
Injection-Date: Sat, 26 Aug 2023 10:44:34 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1589
 by: Öö Tiib - Sat, 26 Aug 2023 10:44 UTC

On Saturday, 26 August 2023 at 13:01:52 UTC+3, wij wrote:
> constexpr const char K1[]="abc";
> constexpr const char K2[]="efg";
> constexpr const char K3[]= ??? // "abcefg" expected
>
> I'd like K3 to be "abcefg", how should I do?

You can not make any functions that return char array (or whatever other
raw array) so there are no way. Raw arrays can't be passed by value in C++.
You can make std::array compile time as it can be passed.

Re: How to concat two constexpr c-string?

<ucclfp$ht9r$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1192&group=comp.lang.c%2B%2B#1192

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: How to concat two constexpr c-string?
Date: Sat, 26 Aug 2023 12:51:39 +0200
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <ucclfp$ht9r$1@dont-email.me>
References: <2eea619a-b7f4-4df1-9bdc-6b2582c03991n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 26 Aug 2023 10:51:37 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="3e6943790182c1617cc610032cf207b9";
logging-data="587067"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+0SuEmicAVUgjOPhZ5bJ1mECC9ShWpljA="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:R0iJd9cKxTeqQGcWSUcjEXweVTY=
In-Reply-To: <2eea619a-b7f4-4df1-9bdc-6b2582c03991n@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Sat, 26 Aug 2023 10:51 UTC

Am 26.08.2023 um 12:01 schrieb wij:
> constexpr const char K1[]="abc";
> constexpr const char K2[]="efg";
> constexpr const char K3[]= ??? // "abcefg" expected
>
> I'd like K3 to be "abcefg", how should I do?

Not quite what you're looking for, but would you be happy with it?

#include <string_view>

constexpr char const *concatted = "abcdefg";
constexpr std::string_view
first( concatted, 3 ),
second( concatted + 3, 3 );

Re: How to concat two constexpr c-string?

<uccmsk$i524$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1193&group=comp.lang.c%2B%2B#1193

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: How to concat two constexpr c-string?
Date: Sat, 26 Aug 2023 13:15:35 +0200
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <uccmsk$i524$1@dont-email.me>
References: <2eea619a-b7f4-4df1-9bdc-6b2582c03991n@googlegroups.com>
<ucclfp$ht9r$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 26 Aug 2023 11:15:33 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="3e6943790182c1617cc610032cf207b9";
logging-data="595012"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+8SiyGsdlmbH/+ZqHfW9QLp26K6ziYu58="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:PBxuav0+9GyL6BWHjypmomx0w00=
Content-Language: de-DE
In-Reply-To: <ucclfp$ht9r$1@dont-email.me>
 by: Bonita Montero - Sat, 26 Aug 2023 11:15 UTC

Am 26.08.2023 um 12:51 schrieb Bonita Montero:
> Am 26.08.2023 um 12:01 schrieb wij:
>> constexpr const char K1[]="abc";
>> constexpr const char K2[]="efg";
>> constexpr const char K3[]= ??? // "abcefg" expected
>>
>> I'd like K3 to be "abcefg", how should I do?
>
> Not quite what you're looking for, but would you be happy with it?
>
> #include <string_view>
>
> constexpr char const *concatted = "abcdefg";
> constexpr std::string_view
>     first( concatted, 3 ),
>     second( concatted + 3, 3 );

Or more C++-ish:

constexpr std::string_view
concatted( "abcdefg" ),
first( concatted.data(), 3 ),
second( concatted.data() + 3, 3 );

Re: How to concat two constexpr c-string?

<0017fc73-4f9e-4a00-988a-939aac029d13n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1194&group=comp.lang.c%2B%2B#1194

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:a05:622a:6f0a:b0:407:2c52:2861 with SMTP id iv10-20020a05622a6f0a00b004072c522861mr323791qtb.8.1693055858680;
Sat, 26 Aug 2023 06:17:38 -0700 (PDT)
X-Received: by 2002:a05:622a:1909:b0:412:d89:eb1d with SMTP id
w9-20020a05622a190900b004120d89eb1dmr368572qtc.5.1693055858431; Sat, 26 Aug
2023 06:17:38 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Sat, 26 Aug 2023 06:17:38 -0700 (PDT)
In-Reply-To: <610a03f1-1ff9-4ef9-b2c5-8634f6cec8d3n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=124.218.76.41; posting-account=0Ek0TQoAAAAS0oceh95IuNV59QuIWNeN
NNTP-Posting-Host: 124.218.76.41
References: <2eea619a-b7f4-4df1-9bdc-6b2582c03991n@googlegroups.com> <610a03f1-1ff9-4ef9-b2c5-8634f6cec8d3n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <0017fc73-4f9e-4a00-988a-939aac029d13n@googlegroups.com>
Subject: Re: How to concat two constexpr c-string?
From: wynii...@gmail.com (wij)
Injection-Date: Sat, 26 Aug 2023 13:17:38 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2741
 by: wij - Sat, 26 Aug 2023 13:17 UTC

On Saturday, August 26, 2023 at 6:44:44 PM UTC+8, Öö Tiib wrote:
> On Saturday, 26 August 2023 at 13:01:52 UTC+3, wij wrote:
> > constexpr const char K1[]="abc";
> > constexpr const char K2[]="efg";
> > constexpr const char K3[]= ??? // "abcefg" expected
> >
> > I'd like K3 to be "abcefg", how should I do?
> You can not make any functions that return char array (or whatever other
> raw array) so there are no way. Raw arrays can't be passed by value in C++.
> You can make std::array compile time as it can be passed.

I saw an example made for C++11, it looked strange... I don't have any idea:
(can I make the initialization of str3 successful?)

//-------------
// copy from https://juejin.cn/s/c%2B%2B%20concat%20constexpr%20string%20literals
//
#include <iostream>

constexpr const char* operator+(const char* lhs, const char* rhs) {
int size_lhs = 0, size_rhs = 0;
while (lhs[size_lhs] != '\0') ++size_lhs;
while (rhs[size_rhs] != '\0') ++size_rhs;

char* result = new char[size_lhs + size_rhs + 1];
int i = 0;
for (; i < size_lhs; ++i) result[i] = lhs[i];
for (int j = 0; j < size_rhs; ++j, ++i) result[i] = rhs[j];
result[i] = '\0';

return result;
}

int main() {
constexpr const char* str1 = "hello";
constexpr const char* str2 = " world";
constexpr const char* str3 = str1 + str2;
std::cout << str3 << std::endl;
return 0;
}

Re: How to concat two constexpr c-string?

<uccu9l$jalu$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1195&group=comp.lang.c%2B%2B#1195

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: How to concat two constexpr c-string?
Date: Sat, 26 Aug 2023 15:22:00 +0200
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <uccu9l$jalu$1@dont-email.me>
References: <2eea619a-b7f4-4df1-9bdc-6b2582c03991n@googlegroups.com>
<610a03f1-1ff9-4ef9-b2c5-8634f6cec8d3n@googlegroups.com>
<0017fc73-4f9e-4a00-988a-939aac029d13n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 26 Aug 2023 13:21:57 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="3e6943790182c1617cc610032cf207b9";
logging-data="633534"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1940z85gB7N+bzaDbV+hn8bz0XLI9beUiU="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:OQrNVodrbNV2VZXKdwXLEie4nOQ=
In-Reply-To: <0017fc73-4f9e-4a00-988a-939aac029d13n@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Sat, 26 Aug 2023 13:22 UTC

Am 26.08.2023 um 15:17 schrieb wij:

> constexpr const char* operator+(const char* lhs, const char* rhs) {
> int size_lhs = 0, size_rhs = 0;
> while (lhs[size_lhs] != '\0') ++size_lhs;
> while (rhs[size_rhs] != '\0') ++size_rhs;
>
> char* result = new char[size_lhs + size_rhs + 1];
> int i = 0;
> for (; i < size_lhs; ++i) result[i] = lhs[i];
> for (int j = 0; j < size_rhs; ++j, ++i) result[i] = rhs[j];
> result[i] = '\0';
>
> return result;
> }

Why do you make a function calling new constexpr ?

Re: How to concat two constexpr c-string?

<ae926eba-05a7-45a8-9c4a-95738bd731a9n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1196&group=comp.lang.c%2B%2B#1196

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:a05:622a:14ce:b0:412:288:8941 with SMTP id u14-20020a05622a14ce00b0041202888941mr438527qtx.6.1693056781507;
Sat, 26 Aug 2023 06:33:01 -0700 (PDT)
X-Received: by 2002:a05:620a:148:b0:76e:1a6a:166 with SMTP id
e8-20020a05620a014800b0076e1a6a0166mr475420qkn.0.1693056781221; Sat, 26 Aug
2023 06:33:01 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Sat, 26 Aug 2023 06:33:00 -0700 (PDT)
In-Reply-To: <uccu9l$jalu$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=124.218.76.41; posting-account=0Ek0TQoAAAAS0oceh95IuNV59QuIWNeN
NNTP-Posting-Host: 124.218.76.41
References: <2eea619a-b7f4-4df1-9bdc-6b2582c03991n@googlegroups.com>
<610a03f1-1ff9-4ef9-b2c5-8634f6cec8d3n@googlegroups.com> <0017fc73-4f9e-4a00-988a-939aac029d13n@googlegroups.com>
<uccu9l$jalu$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ae926eba-05a7-45a8-9c4a-95738bd731a9n@googlegroups.com>
Subject: Re: How to concat two constexpr c-string?
From: wynii...@gmail.com (wij)
Injection-Date: Sat, 26 Aug 2023 13:33:01 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2198
 by: wij - Sat, 26 Aug 2023 13:33 UTC

On Saturday, August 26, 2023 at 9:22:18 PM UTC+8, Bonita Montero wrote:
> Am 26.08.2023 um 15:17 schrieb wij:
>
> > constexpr const char* operator+(const char* lhs, const char* rhs) {
> > int size_lhs = 0, size_rhs = 0;
> > while (lhs[size_lhs] != '\0') ++size_lhs;
> > while (rhs[size_rhs] != '\0') ++size_rhs;
> >
> > char* result = new char[size_lhs + size_rhs + 1];
> > int i = 0;
> > for (; i < size_lhs; ++i) result[i] = lhs[i];
> > for (int j = 0; j < size_rhs; ++j, ++i) result[i] = rhs[j];
> > result[i] = '\0';
> >
> > return result;
> > }
> Why do you make a function calling new constexpr ?

1. I am not familiar with "constexpr"
2. The example is found on https://juejin.cn/s/c%2B%2B%20concat%20constexpr%20string%20literals , not made by me
3. I want the initialization effect like the str3 in the example

Re: How to concat two constexpr c-string?

<ucd0bb$jjoa$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1197&group=comp.lang.c%2B%2B#1197

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: How to concat two constexpr c-string?
Date: Sat, 26 Aug 2023 15:57:01 +0200
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <ucd0bb$jjoa$1@dont-email.me>
References: <2eea619a-b7f4-4df1-9bdc-6b2582c03991n@googlegroups.com>
<610a03f1-1ff9-4ef9-b2c5-8634f6cec8d3n@googlegroups.com>
<0017fc73-4f9e-4a00-988a-939aac029d13n@googlegroups.com>
<uccu9l$jalu$1@dont-email.me>
<ae926eba-05a7-45a8-9c4a-95738bd731a9n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 26 Aug 2023 13:56:59 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="3e6943790182c1617cc610032cf207b9";
logging-data="642826"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19z/oOxnwS5xceAWuoLPBV8F4uhmzPDXpI="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:V55ZhJvoMfuwa/7b4Tzap3Tv/eY=
Content-Language: de-DE
In-Reply-To: <ae926eba-05a7-45a8-9c4a-95738bd731a9n@googlegroups.com>
 by: Bonita Montero - Sat, 26 Aug 2023 13:57 UTC

Am 26.08.2023 um 15:33 schrieb wij:

> 1. I am not familiar with "constexpr"

There are three constexprs:
3. If you declare a function constexpr the compiler is requested
to compile-time evaluate the function if possible. If you have a
dynamic memory allocation that couldn't be compile-time evaluateable.
1. If you declare a variable constexpr the value assigned to it must
be compile-time evaluateable. Functions used to initialize the variable
must be constexpr and actually compile-time evaluatable.
2. If you have an if constexpr( expr ) expr must be compile-time
evaluateable. Functions called by expr must be also compie-time
evaluateable like with 2.

Re: How to concat two constexpr c-string?

<ucdls9$o9q0$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1205&group=comp.lang.c%2B%2B#1205

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.hispagatos.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: alf.p.st...@gmail.com (Alf P. Steinbach)
Newsgroups: comp.lang.c++
Subject: Re: How to concat two constexpr c-string?
Date: Sat, 26 Aug 2023 22:04:23 +0200
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <ucdls9$o9q0$1@dont-email.me>
References: <2eea619a-b7f4-4df1-9bdc-6b2582c03991n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 26 Aug 2023 20:04:25 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="96a5cb2f2eccc72b84572fa92fcba41f";
logging-data="796480"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19uTy8crSM6zLEQVeDjgrZx"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.14.0
Cancel-Lock: sha1:pDx9UBv8s6eIiJwzWFmxLo8924Q=
Content-Language: en-US
In-Reply-To: <2eea619a-b7f4-4df1-9bdc-6b2582c03991n@googlegroups.com>
 by: Alf P. Steinbach - Sat, 26 Aug 2023 20:04 UTC

On 2023-08-26 12:01 PM, wij wrote:
> constexpr const char K1[]="abc";
> constexpr const char K2[]="efg";
> constexpr const char K3[]= ??? // "abcefg" expected
>
> I'd like K3 to be "abcefg", how should I do?

It depends on your requirements.

If you're free to use the preprocessor you can do simply

#define K1 "abc"
#define K2 "efg"
#define K3 K1 K2

constexpr auto& k1 = K1;
constexpr auto& k2 = K2;
constexpr auto& k3 = K3;

Easy peasy. :-)

- Alf

Re: How to concat two constexpr c-string?

<ueot4e$19hia$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1789&group=comp.lang.c%2B%2B#1789

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: kerrttuP...@gmail.com (JiiPee)
Newsgroups: comp.lang.c++
Subject: Re: How to concat two constexpr c-string?
Date: Sun, 24 Sep 2023 11:48:14 +0300
Organization: A noiseless patient Spider
Lines: 5
Message-ID: <ueot4e$19hia$1@dont-email.me>
References: <2eea619a-b7f4-4df1-9bdc-6b2582c03991n@googlegroups.com>
<610a03f1-1ff9-4ef9-b2c5-8634f6cec8d3n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 24 Sep 2023 08:48:14 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d08bfba04dd716173d6d40fed9bfd0e8";
logging-data="1361482"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ccvOEer8qRdpaJ2b6Ospf"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:b3ZVPSmODQ/R8hwQAORdRroAE+g=
In-Reply-To: <610a03f1-1ff9-4ef9-b2c5-8634f6cec8d3n@googlegroups.com>
 by: JiiPee - Sun, 24 Sep 2023 08:48 UTC

On 26/08/2023 13:44, Öö Tiib wrote:
> You can make std::array compile time as it can be passed.

Am I right that in this kind of situationsa std::array is recommended
rather than C arrays?

Re: How to concat two constexpr c-string?

<ueotfv$19hia$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1790&group=comp.lang.c%2B%2B#1790

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: kerrttuP...@gmail.com (JiiPee)
Newsgroups: comp.lang.c++
Subject: Re: How to concat two constexpr c-string?
Date: Sun, 24 Sep 2023 11:54:23 +0300
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <ueotfv$19hia$2@dont-email.me>
References: <2eea619a-b7f4-4df1-9bdc-6b2582c03991n@googlegroups.com>
<ucclfp$ht9r$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 24 Sep 2023 08:54:23 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d08bfba04dd716173d6d40fed9bfd0e8";
logging-data="1361482"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18wD2s5pn1v+WKFAJIGQja0"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:HdjU0qCiWoLxBZS9QA+PR8Hl5J4=
In-Reply-To: <ucclfp$ht9r$1@dont-email.me>
 by: JiiPee - Sun, 24 Sep 2023 08:54 UTC

On 26/08/2023 13:51, Bonita Montero wrote:
> Am 26.08.2023 um 12:01 schrieb wij:
>> constexpr const char K1[]="abc";
>> constexpr const char K2[]="efg";
>> constexpr const char K3[]= ??? // "abcefg" expected
>>
>> I'd like K3 to be "abcefg", how should I do?
>
> Not quite what you're looking for, but would you be happy with it?
>
> #include <string_view>
>
> constexpr char const *concatted = "abcdefg";
> constexpr std::string_view
>     first( concatted, 3 ),
>     second( concatted + 3, 3 );

ye that view is surely good in this kind of things. I kinda "forgot" to
use it, should maybe use it more to become second nature.

Re: How to concat two constexpr c-string?

<ueoti8$19hia$3@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1791&group=comp.lang.c%2B%2B#1791

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: kerrttuP...@gmail.com (JiiPee)
Newsgroups: comp.lang.c++
Subject: Re: How to concat two constexpr c-string?
Date: Sun, 24 Sep 2023 11:55:36 +0300
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <ueoti8$19hia$3@dont-email.me>
References: <2eea619a-b7f4-4df1-9bdc-6b2582c03991n@googlegroups.com>
<ucdls9$o9q0$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 24 Sep 2023 08:55:36 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d08bfba04dd716173d6d40fed9bfd0e8";
logging-data="1361482"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+2fnTLXWhBITdLqjOFi9f/"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:7FVEEL4+P0ix4RsIDsg2M9otsqQ=
In-Reply-To: <ucdls9$o9q0$1@dont-email.me>
 by: JiiPee - Sun, 24 Sep 2023 08:55 UTC

On 26/08/2023 23:04, Alf P. Steinbach wrote:
> On 2023-08-26 12:01 PM, wij wrote:
>> constexpr const char K1[]="abc";
>> constexpr const char K2[]="efg";
>> constexpr const char K3[]= ??? // "abcefg" expected
>>
>> I'd like K3 to be "abcefg", how should I do?
>
> It depends on your requirements.
>
> If you're free to use the preprocessor you can do simply
>
>
>     #define K1 "abc"
>     #define K2 "efg"
>     #define K3 K1 K2
>
>     constexpr auto& k1 = K1;
>     constexpr auto& k2 = K2;
>     constexpr auto& k3 = K3;
>
>
> Easy peasy. :-)
>
> - Alf
>

But isnt it that many recommend not to use define's?

Re: How to concat two constexpr c-string?

<uep139$1a5ef$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1793&group=comp.lang.c%2B%2B#1793

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c++
Subject: Re: How to concat two constexpr c-string?
Date: Sun, 24 Sep 2023 11:55:53 +0200
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <uep139$1a5ef$1@dont-email.me>
References: <2eea619a-b7f4-4df1-9bdc-6b2582c03991n@googlegroups.com>
<610a03f1-1ff9-4ef9-b2c5-8634f6cec8d3n@googlegroups.com>
<ueot4e$19hia$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 24 Sep 2023 09:55:53 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="38e2ca41c0989e98929244a8cf510f69";
logging-data="1381839"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX199UXyPiKqvKJgmerDDltJXJrJ9mwQolBA="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.13.0
Cancel-Lock: sha1:hAJMzxkBxAMXTOB6asj0tBlX/QI=
Content-Language: en-GB
In-Reply-To: <ueot4e$19hia$1@dont-email.me>
 by: David Brown - Sun, 24 Sep 2023 09:55 UTC

On 24/09/2023 10:48, JiiPee wrote:
> On 26/08/2023 13:44, Öö Tiib wrote:
>> You can make std::array compile time as it can be passed.
>
> Am I right that in this kind of situationsa std::array is recommended
> rather than C arrays?

When you have a fixed compile-time size for an array, I prefer
std::array, as it makes the array a "solid" object. (In C, I wrap the
array in a struct.) You can pass it by value, assign it, and so on.
You can use it like a container with some of the common container
methods and functions, for loops, and that kind of thing.

The key disadvantage of std::array is that since the size is part of the
type, you can't make a single non-template function that takes arrays of
different sizes as parameters. If that is not an issue for a particular
use-case, then std::array is often a nicer choice.

Re: How to concat two constexpr c-string?

<ueqc1r$1hn19$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1830&group=comp.lang.c%2B%2B#1830

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: alf.p.st...@gmail.com (Alf P. Steinbach)
Newsgroups: comp.lang.c++
Subject: Re: How to concat two constexpr c-string?
Date: Mon, 25 Sep 2023 00:08:57 +0200
Organization: A noiseless patient Spider
Lines: 88
Message-ID: <ueqc1r$1hn19$1@dont-email.me>
References: <2eea619a-b7f4-4df1-9bdc-6b2582c03991n@googlegroups.com>
<ucdls9$o9q0$1@dont-email.me> <ueoti8$19hia$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 24 Sep 2023 22:08:59 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="ec66c3de1e6b831538784794fd6775cd";
logging-data="1629225"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19uB5aF8kKg8FdqZfEJJyPm"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:B6/g78GkGR3OWFqNOim5t7ks83M=
Content-Language: en-US
In-Reply-To: <ueoti8$19hia$3@dont-email.me>
 by: Alf P. Steinbach - Sun, 24 Sep 2023 22:08 UTC

On 2023-09-24 10:55 AM, JiiPee wrote:
> On 26/08/2023 23:04, Alf P. Steinbach wrote:
>> On 2023-08-26 12:01 PM, wij wrote:
>>> constexpr const char K1[]="abc";
>>> constexpr const char K2[]="efg";
>>> constexpr const char K3[]= ??? // "abcefg" expected
>>>
>>> I'd like K3 to be "abcefg", how should I do?
>>
>> It depends on your requirements.
>>
>> If you're free to use the preprocessor you can do simply
>>
>>
>>      #define K1 "abc"
>>      #define K2 "efg"
>>      #define K3 K1 K2
>>
>>      constexpr auto& k1 = K1;
>>      constexpr auto& k2 = K2;
>>      constexpr auto& k3 = K3;
>>
>>
>> Easy peasy. :-)
>>
>> - Alf
>>
>
> But isnt it that many recommend not to use define's?

Yes. But with proper prefixes it's quite clean. Unfortunately the
standard language doesn't yet support pushing/popping macro definitions,
though individual implementations do as language extensions.

If you really want to avoid macros, /and/ want that compile time
concatenation really bad, consider something like

---
namespace my {
template< class Type > using in_ = const Type&;
template< class Type > using ref_ = Type&;

template< int n, class Item >
struct Wrapped_array_
{
using Raw = Item[n];
Raw items;

constexpr auto raw() const -> ref_<const Raw> { return items; }
};

template< int size_a, int size_b >
constexpr auto adjoined( in_<const char[size_a]> a, in_<const
char[size_b]> b )
-> Wrapped_array_<size_a + size_b - 1, char>
{
const int len_a = size_a - 1;
const int len_b = size_b - 1;
const int n = len_a + len_b + 1;

Wrapped_array_<n, char> result{};

for( int i = 0; i < len_a; ++i ) {
result.items[i] = a[i];
}
for( int i = 0; i < len_b; ++i ) {
result.items[i + len_a] = b[i];
}
result.items[n - 1] = '\0';
return result;
}
} // namespace my

#include <stdio.h>
auto main() -> int
{ constexpr auto& a = "abc";
constexpr auto& b = "def";
static constexpr auto c_wrapped = my::adjoined( a, b );
constexpr auto& c = c_wrapped.raw();

puts( c );
} ---

- Alf


devel / comp.lang.c++ / How to concat two constexpr c-string?

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor