Hi, is a comma required for initializing a std::string array?
This compiles without error on Visual C++ 2015:
std::string strings [5] = { " " "1" "3432" "2244" "0" };\
Hi, is a comma required for initializing a std::string array?
This compiles without error on Visual C++ 2015:
std::string strings [5] = { " " "1" "3432" "2244" "0" };
On 2026-06-11 19:43, Lynn McGuire wrote:
Hi, is a comma required for initializing a std::string array?
This compiles without error on Visual C++ 2015:
std::string strings [5] = { " " "1" "3432" "2244" "0" };\
Those string literals get catenated during translation phase 5, so the
above is equivalent to
std::string strings[5] = {" 1343222440"};
I doubt that is what you intended. String literal catenation can be very useful in certain contexts, but it creates a trap for the unwary.
Lynn McGuire <lynnmcguire5@gmail.com> writes:
Hi, is a comma required for initializing a std::string array?
This compiles without error on Visual C++ 2015:
std::string strings [5] = { " " "1" "3432" "2244" "0" };
It compiles, but it doesn't mean what you probably think it means.
Adjacent string literals are concatenated in translation phase 5,
which is typically part of the preprocessor. (C does the same thing
in translation phase 6.) (This allows a program to have very long
strings without exceeding line length limitations or using ugly
backslash line continuation.)
So your declaration is equivalent to:
std::string strings [5] = { " 1343222440" };
The string literal is used to initialize strings[0], and the other 4
elements are default-initialized.
Yeah, you need the commas.
Hi, is a comma required for initializing a std::string array?"Not" " For " "This"
This compiles without error on Visual C++ 2015:
std::string strings [5] = { " " "1" "3432" "2244" "0" };
On 6/11/2026 4:43 PM, Lynn McGuire wrote:
Hi, is a comma required for initializing a std::string array?"Not" " For " "This"
This compiles without error on Visual C++ 2015:
std::string strings [5] = { " " "1" "3432" "2244" "0" };
Hi, is a comma required for initializing a std::string array?
This compiles without error on Visual C++ 2015:
std::string strings [5] = { " " "1" "3432" "2244" "0" };
Am 12.06.2026 um 01:43 schrieb Lynn McGuire:
Hi, is a comma required for initializing a std::string array?
This compiles without error on Visual C++ 2015:
std::string strings [5] = { " " "1" "3432" "2244" "0" };
This is currently planned for C++28.
On 6/12/2026 2:58 AM, Bonita Montero wrote:
Am 12.06.2026 um 01:43 schrieb Lynn McGuire:
Hi, is a comma required for initializing a std::string array?This is currently planned for C++28.
This compiles without error on Visual C++ 2015:
std::string strings [5] = { " " "1" "3432" "2244" "0" };
A new error or to initialize the string array properly ?
Got a URL ?
Lynn McGuire <lynnmcguire5@gmail.com> writes:
On 6/12/2026 2:58 AM, Bonita Montero wrote:
Am 12.06.2026 um 01:43 schrieb Lynn McGuire:
Hi, is a comma required for initializing a std::string array?This is currently planned for C++28.
This compiles without error on Visual C++ 2015:
std::string strings [5] = { " " "1" "3432" "2244" "0" };
A new error or to initialize the string array properly ?
Got a URL ?
I'm skeptical that there's any planned change that would change the
semantics of the above declaration. Concatenation of string literals
happens in an early translation phase. The above declaration is
already perfectly meaningful; quietly changing its meaning is not
a viable option.
Looking through the git repo for the C++ standard, I see some
discussion about concatenation of string literals with different
prefixes, such as "Lfoo" "u"bar", but nothing relevant here.
(I don't read Bonita's posts, so if she responds I won't see it.)
Even if there were such a proposal, fixing the bug in your Fortran to
C++ translator so it adds the commas would still be a better solution.
(I'm assuming it's a bug, but I can't be sure without seeing and understanding the input Fortran code.)
On 6/12/2026 4:51 PM, Keith Thompson wrote:
Lynn McGuire <lynnmcguire5@gmail.com> writes:
On 6/12/2026 2:58 AM, Bonita Montero wrote:I'm skeptical that there's any planned change that would change the
Am 12.06.2026 um 01:43 schrieb Lynn McGuire:
Hi, is a comma required for initializing a std::string array?This is currently planned for C++28.
This compiles without error on Visual C++ 2015:
std::string strings [5] = { " " "1" "3432" "2244" "0" };
A new error or to initialize the string array properly ?
Got a URL ?
semantics of the above declaration. Concatenation of string literals
happens in an early translation phase. The above declaration is
already perfectly meaningful; quietly changing its meaning is not
a viable option.
Looking through the git repo for the C++ standard, I see some
discussion about concatenation of string literals with different
prefixes, such as "Lfoo" "u"bar", but nothing relevant here.
(I don't read Bonita's posts, so if she responds I won't see it.)
Even if there were such a proposal, fixing the bug in your Fortran
to
C++ translator so it adds the commas would still be a better solution.
(I'm assuming it's a bug, but I can't be sure without seeing and
understanding the input Fortran code.)
I used the F2C translator with many changes on my part. F2C does not
handle Fortran character strings very well at all.
Here is my Fortran code:
character*4 IBNK (9)
DATA (IBNK (I), I = 1, 9)
1 / 'CPTB', 'VPTB', 'LVTB', 'LHTB', 'STTB', 'VSTB',
* 'VSTB', 'TCTB', 'TCTB' /
Here is my translated C++ code from my F2C:
static char ibnk[4*9] = "CPTB" "VPTB" "LVTB" "LHTB" "STTB" "VSTB"
"VSTB" "TCTB" "TCTB";
Here is the code I ended up with:
const int numItems = 9;
static std::string ibnk[numItems] = { "CPTB", "VPTB", "LVTB",
"LHTB", "STTB", "VSTB", "VSTB", "TCTB", "TCTB" };
Lynn McGuire <lynnmcguire5@gmail.com> writes:
On 6/12/2026 4:51 PM, Keith Thompson wrote:
Lynn McGuire <lynnmcguire5@gmail.com> writes:
On 6/12/2026 2:58 AM, Bonita Montero wrote:I'm skeptical that there's any planned change that would change the
Am 12.06.2026 um 01:43 schrieb Lynn McGuire:
Hi, is a comma required for initializing a std::string array?This is currently planned for C++28.
This compiles without error on Visual C++ 2015:
std::string strings [5] = { " " "1" "3432" "2244" "0" };
A new error or to initialize the string array properly ?
Got a URL ?
semantics of the above declaration. Concatenation of string literals
happens in an early translation phase. The above declaration is
already perfectly meaningful; quietly changing its meaning is not
a viable option.
Looking through the git repo for the C++ standard, I see some
discussion about concatenation of string literals with different
prefixes, such as "Lfoo" "u"bar", but nothing relevant here.
(I don't read Bonita's posts, so if she responds I won't see it.)
Even if there were such a proposal, fixing the bug in your Fortran
to
C++ translator so it adds the commas would still be a better solution.
(I'm assuming it's a bug, but I can't be sure without seeing and
understanding the input Fortran code.)
I used the F2C translator with many changes on my part. F2C does not
handle Fortran character strings very well at all.
Here is my Fortran code:
character*4 IBNK (9)
DATA (IBNK (I), I = 1, 9)
1 / 'CPTB', 'VPTB', 'LVTB', 'LHTB', 'STTB', 'VSTB',
* 'VSTB', 'TCTB', 'TCTB' /
Here is my translated C++ code from my F2C:
static char ibnk[4*9] = "CPTB" "VPTB" "LVTB" "LHTB" "STTB" "VSTB"
"VSTB" "TCTB" "TCTB";
That's almost valid C++ code, but with a very different meaning
than the Fortran (if I understand it correctly) and from what you
intended. It defines a 1-dimensional array of characters with the 4-character strings just concatenated together. I guess Fortran
doesn't use null characters as string terminators.
It would be valid in C, where, as a special case, this:
char foo[3] = "foo";
doesn't store the trailing '\0'. C++ doesn't have that rule, so the initializer is too long.
It looks like you're having to do a lot of manual cleanup. I don't
know whether it would be worthwhile to fix the translator so the
generated code is closer to being correct. If the goal is discard
(or at least archive) the Fortran code and translator once you have
a working C++ version, it might not be. But having it insert
those commas automatically might be a good thing.
Here is the code I ended up with:
const int numItems = 9;
static std::string ibnk[numItems] = { "CPTB", "VPTB", "LVTB",
"LHTB", "STTB", "VSTB", "VSTB", "TCTB", "TCTB" };
constexpr might be cleaner than const if you're using C++11 or later.
Another alternative might be:
static const char *const ibnk[] = {
"CPTB", "VPTB", "LVTB",
"LHTB", "STTB", "VSTB",
"VSTB", "TCTB", "TCTB"
};
if you don't need the extra features of std::string.
Even if there were such a proposal, fixing the bug in your Fortran to
C++ translator ...
Am 12.06.2026 um 23:51 schrieb Keith Thompson:
Even if there were such a proposal, fixing the bug in your Fortran to
C++ translator ...
Lynn, do you really get pragmatic and readable C++ code with that ?
I used the F2C translator with many changes on my part. F2C does notIs it possible that one of your changes broke something? That would not
handle Fortran character strings very well at all.
Here is my Fortran code:
character*4 IBNK (9)
DATA (IBNK (I), I = 1, 9)
1 / 'CPTB', 'VPTB', 'LVTB', 'LHTB', 'STTB', 'VSTB',
* 'VSTB', 'TCTB', 'TCTB' /
Here is my translated C++ code from my F2C:
static char ibnk[4*9] = "CPTB" "VPTB" "LVTB" "LHTB" "STTB" "VSTB" "VSTB" "TCTB" "TCTB";
On 2026-06-12 18:35, Lynn McGuire wrote:
...
I used the F2C translator with many changes on my part. F2C does not
handle Fortran character strings very well at all.
Here is my Fortran code:
character*4 IBNK (9)
DATA (IBNK (I), I = 1, 9)
1 / 'CPTB', 'VPTB', 'LVTB', 'LHTB', 'STTB', 'VSTB',
* 'VSTB', 'TCTB', 'TCTB' /
Here is my translated C++ code from my F2C:
static char ibnk[4*9] = "CPTB" "VPTB" "LVTB" "LHTB" "STTB" "VSTB"
"VSTB" "TCTB" "TCTB";
Is it possible that one of your changes broke something? That would not
be an accurate translation of the fortran code for any version of C, not
even the oldest.
On 2026-06-12 18:35, Lynn McGuire wrote:
...
I used the F2C translator with many changes on my part. F2C does notIs it possible that one of your changes broke something? That would not
handle Fortran character strings very well at all.
Here is my Fortran code:
character*4 IBNK (9)
DATA (IBNK (I), I = 1, 9)
1 / 'CPTB', 'VPTB', 'LVTB', 'LHTB', 'STTB', 'VSTB',
* 'VSTB', 'TCTB', 'TCTB' /
Here is my translated C++ code from my F2C:
static char ibnk[4*9] = "CPTB" "VPTB" "LVTB" "LHTB" "STTB" "VSTB"
"VSTB" "TCTB" "TCTB";
be an accurate translation of the fortran code for any version of C, not
even the oldest.
On 6/12/2026 2:58 AM, Bonita Montero wrote:
Am 12.06.2026 um 01:43 schrieb Lynn McGuire:
Hi, is a comma required for initializing a std::string array?
This compiles without error on Visual C++ 2015:
std::string strings [5] = { " " "1" "3432" "2244" "0" };
This is currently planned for C++28.
A new error or to initialize the string array properly ?
Got a URL ?
James Kuyper <jameskuyper@alumni.caltech.edu> writes:
On 2026-06-12 18:35, Lynn McGuire wrote:
...
I used the F2C translator with many changes on my part. F2C does not
handle Fortran character strings very well at all.
Here is my Fortran code:
character*4 IBNK (9)
DATA (IBNK (I), I = 1, 9)
1 / 'CPTB', 'VPTB', 'LVTB', 'LHTB', 'STTB', 'VSTB',
* 'VSTB', 'TCTB', 'TCTB' /
Here is my translated C++ code from my F2C:
static char ibnk[4*9] = "CPTB" "VPTB" "LVTB" "LHTB" "STTB" "VSTB"
"VSTB" "TCTB" "TCTB";
Is it possible that one of your changes broke something? That would not
be an accurate translation of the fortran code for any version of C, not
even the oldest.
No, the original f2c does this -- and it appears to be correct.
I think f2c is designed to generate C code that behaves the same
way as its input Fortran code, *not* to generate C code that's
expected to be maintained in that form. A tool to do the latter
could be very useful, but probably much harder to implement.
I was able to turn the above Fortan into a complete working program.
(Without the print, f2c eliminated the declaration of IBNK, since
it was unused.)
program lynn
character*4 IBNK (9)
DATA (IBNK (I), I = 1, 9)
1 / 'CPTB', 'VPTB', 'LVTB', 'LHTB', 'STTB', 'VSTB',
* 'VSTB', 'TCTB', 'TCTB' /
print '(a)', IBNK(2)
end
When I compile it with gfortran, the output is:
VPTB
which is what I expected. When I feed to f2c version 20200916, the
generated C code includes the following:
static char ibnk[4*9] = "CPTB" "VPTB" "LVTB" "LHTB" "STTB" "VSTB" "VSTB"
"TCTB" "TCTB";
But it also includes this:
do_fio(&c__1, ibnk + 4, (ftnlen)4);
which looks (to me) like correct code to extract and print the
characters "VPTB" from the char array ibnk.
f2c might sensibly have used a single string literal "CPTBVPTBLVTBLHTBSTTBVSTBVSTBTCTBTCTB", but that could run into
problems with line length.
I haven't been able (after not trying very hard) to get the generated
C code to compile and execute correctly.
I *think* the Fortran declaration of IBNK specifies that it contains
the consecutive characters "CPTBVPTBLVTBLHTBSTTBVSTBVSTBTCTBTCTB",
and any code that extracts a 4-character element has to know the
dimensions. The generated C code appears to do the same thing,
correctly as far as I can tell.
The problem is that the generated C code is very un-C-like, and even
more un-C++-like -- and apparently Lynn was (quite understandably)
thrown off by the odd use of string literal concatenation. Changing
this automatically generated C written with a strong Fortran accent
to C++ code using std::string almost has to be done manually, while
referring back to the input Fortran code to understand how it's
supposed to behave. Doing that reliably requires a strong knowledge
of both Fortran and C++, and even so is likely to be error-prone.
Lynn McGuire wrote this screed in ALL-CAPS:
On 6/12/2026 2:58 AM, Bonita Montero wrote:
Am 12.06.2026 um 01:43 schrieb Lynn McGuire:
Hi, is a comma required for initializing a std::string array?
This compiles without error on Visual C++ 2015:
std::string strings [5] = { " " "1" "3432" "2244" "0" };
That looks to me to be " 1343222440" plus 4 empty strings.
This is currently planned for C++28.
Sounds screwy to me, given that adjacent quotes strings are
combined into one, a feature I use a lot..
A new error or to initialize the string array properly ?
Got a URL ?
On 6/13/2026 7:52 PM, Keith Thompson wrote:
And, I am converting the string writes to typesafe code in C++ which is >worth quite a bit to me in code safety. I really hate it when a program
is crashing and goes to an error statement and does a hard crash there
in a call to printf or sprintf. Using type safe code in C++ stops hard >crashes.
Lynn McGuire <lynnmcguire5@gmail.com> writes:
On 6/13/2026 7:52 PM, Keith Thompson wrote:
And, I am converting the string writes to typesafe code in C++ which is
worth quite a bit to me in code safety. I really hate it when a program
is crashing and goes to an error statement and does a hard crash there
in a call to printf or sprintf. Using type safe code in C++ stops hard
crashes.
gcc (and g++) have supported type-safe printf/snprintf (and optionally
any function using printf/scanf style format strings) for a couple of decades.
Avoiding printf/snprintf using C++ '<<' redirection requires signficantly more source _and_ object code than using the *printf family functions.
Lynn McGuire <lynnmcguire5@gmail.com> writes:
On 6/13/2026 7:52 PM, Keith Thompson wrote:
And, I am converting the string writes to typesafe code in C++ which is >>worth quite a bit to me in code safety. I really hate it when a program >>is crashing and goes to an error statement and does a hard crash there
in a call to printf or sprintf. Using type safe code in C++ stops hard >>crashes.
gcc (and g++) have supported type-safe printf/snprintf (and optionally
any function using printf/scanf style format strings) for a couple of decades.
scott@slp53.sl.home (Scott Lurndal) writes:
Lynn McGuire <lynnmcguire5@gmail.com> writes:
On 6/13/2026 7:52 PM, Keith Thompson wrote:
And, I am converting the string writes to typesafe code in C++ which is
worth quite a bit to me in code safety. I really hate it when a program >>> is crashing and goes to an error statement and does a hard crash there
in a call to printf or sprintf. Using type safe code in C++ stops hard
crashes.
gcc (and g++) have supported type-safe printf/snprintf (and optionally
any function using printf/scanf style format strings) for a couple of
decades.
Are you referring to the warnings that gcc issues for mismatched
printf arguments (only if the format string is a literal), or is
there some other feature I don't know about?
On 16/06/2026 02:05, Keith Thompson wrote:
scott@slp53.sl.home (Scott Lurndal) writes:
Lynn McGuire <lynnmcguire5@gmail.com> writes:
On 6/13/2026 7:52 PM, Keith Thompson wrote:
And, I am converting the string writes to typesafe code in C++ which is >>>> worth quite a bit to me in code safety. I really hate it when a program >>>> is crashing and goes to an error statement and does a hard crash there >>>> in a call to printf or sprintf. Using type safe code in C++ stops hard >>>> crashes.
gcc (and g++) have supported type-safe printf/snprintf (and optionally
any function using printf/scanf style format strings) for a couple of
decades.
Are you referring to the warnings that gcc issues for mismatched
printf arguments (only if the format string is a literal), or is
there some other feature I don't know about?
I don't know of anything beyond the format warnings in gcc, but it is
quite possible to use them in connection with non-literal format strings.
A typical case is when you are doing internationalisation. You might
have something like :
On Tue, 16 Jun 2026 10:31:02 +0200
David Brown <david.brown@hesbynett.no> gabbled:
On 16/06/2026 02:05, Keith Thompson wrote:
scott@slp53.sl.home (Scott Lurndal) writes:
Lynn McGuire <lynnmcguire5@gmail.com> writes:
On 6/13/2026 7:52 PM, Keith Thompson wrote:
And, I am converting the string writes to typesafe code in C++
which is
worth quite a bit to me in code safety.ÿ I really hate it when a
program
is crashing and goes to an error statement and does a hard crash there >>>>> in a call to printf or sprintf.ÿ Using type safe code in C++ stops
hard
crashes.
gcc (and g++) have supported type-safe printf/snprintf (and optionally >>>> any function using printf/scanf style format strings) for a couple of
decades.
Are you referring to the warnings that gcc issues for mismatched
printf arguments (only if the format string is a literal), or is
there some other feature I don't know about?
I don't know of anything beyond the format warnings in gcc, but it is
quite possible to use them in connection with non-literal format strings.
A typical case is when you are doing internationalisation.ÿ You might
have something like :
Another case is when doing safe output of strings so embedded % codes can't cause a security issue. ie:
printf("%s",some_unverified_string)
scott@slp53.sl.home (Scott Lurndal) writes:
Lynn McGuire <lynnmcguire5@gmail.com> writes:
On 6/13/2026 7:52 PM, Keith Thompson wrote:
And, I am converting the string writes to typesafe code in C++ which is >>>worth quite a bit to me in code safety. I really hate it when a program >>>is crashing and goes to an error statement and does a hard crash there >>>in a call to printf or sprintf. Using type safe code in C++ stops hard >>>crashes.
gcc (and g++) have supported type-safe printf/snprintf (and optionally
any function using printf/scanf style format strings) for a couple of
decades.
Are you referring to the warnings that gcc issues for mismatched
printf arguments (only if the format string is a literal)
I think what Scott is getting at is...
some_function()
{
ÿÿ // build a format string here
ÿÿ const char *format;
ÿÿ format = <some expression that builds a format string>
ÿÿ printf(format, <some value list>);
}
isn't type checked.
| Sysop: | Tetrazocine |
|---|---|
| Location: | Melbourne, VIC, Australia |
| Users: | 11 |
| Nodes: | 8 (0 / 8) |
| Uptime: | 73:55:34 |
| Calls: | 219 |
| Files: | 21,505 |
| Messages: | 83,640 |