We have two C-strings and for their concatenation we need to know the size of resulting string. And we want to calculate it at compilation stage. How can I improve this solution? char s1[] = string1;char s2[] = string2;constexpr auto SIZE = sizeof(s1) / sizeof(*s1) + sizeof(s2) / sizeof(*s2) + 2; // + for space and \0char ss[SIZE];
Handling C-like strings in C++11
c++;c;strings;c++11
You could use a macro, for example one of these ones.And you shouldn't need to add 2: because sizeof should already include the terminating '\0' of each string. However you could add a comment to explain why you don't add anything: you have two strings each with an implicit terminating '\0' ... one of them terminates the concatenated string, and the other is room for the intervening space: that is, if you think that concatenating strings should add a space, e.g. string1 plus string2 becomes string1 string2. Note that normally, concatenation means that the result is string1string2, in which case you need to subtract 1.Your question isn't very clear. You should provide some test cases which demonstrate the expected result and actual result.