c++ - How to cleanly use: const char* and std::string? -
tl:dr
how can concatenate
const char*std::string, neatly , elegantly, without multiple function calls. ideally in 1 function call , have outputconst char*. impossible, optimum solution?
initial problem
the biggest barrier have experienced c++ far how handles strings. in opinion, of used languages, handles strings poorly. i've seen other questions similar either have answer saying "use std::string" or point out 1 of options going best situation.
however useless advice when trying use strings dynamically how used in other languages. cannot guaranty able use std::string , times when have use const char* hit obvious wall of "it's constant, can't concatenate it".
every solution string manipulation problem i've seen in c++ requires repetitive multiple lines of code work format of string. want able concatenate set of characters + symbol or make use of simple format() function how can in c# or python. why there no easy option?
current situation
standard output
i'm writing dll , far i've been output text cout via << operator. has been going fine far using simple char arrays in form:
cout << "hello world!" runtime strings
now comes point want construct string @ runtime , store class, class hold string reports on errors can picked other classes , maybe sent cout later, string set function setreport(const char* report). don't want use more 1 line go ahead , write like:
setreport("failure in " + __function__ + ": foobar " + foobar + "\n"); // __function__ gets name of current function, foobar variable immediately of course get:
expression must have integral or unscoped enum typeand...'+': cannot add 2 pointers
ugly strings
right. i'm trying add 2 or more const char*s , isn't option. find main suggestion here use std::string, sort of weird typing "hello world!" doesn't give 1 of in first place let's give go:
setreport(std::string("failure in ") + std::string(__function__) + std::string(": foobar ") + std::to_string(foobar) + std::string("\n")); brilliant! works! how ugly is!! that's of ugliest code i've every seen. can simplify this:
setreport(std::string("failure in ") + __function__ + ": foobar " + std::to_string(foobar) + "\n"); still possibly worst way i've every encounter of getting simple 1 line string concatenation should fine right?
convert constant
well no, if you're working on dll, tend lot because unit test need c++ code imported unit test library, find when try set report string member variable of class std::string compiler throws warning saying:
warning c4251: class 'std::basic_string<_elem,_traits,_alloc>' needs have dll-interface used clients of class' the real solution problem i've found other "ignore warning"(bad practice!) use const char* member variable rather std::string not solution, because have convert ugly concatenated (but dynamic) string const char array need. can't tag .c_str() on end (even though why want because concatenation becoming more ridiculous second?) have make sure std::string doesn't clean newly constructed string , leave garbage. have inside function receives string:
const std::string conststring = (input); m_constchar = conststring.c_str(); which insane. because traipsed across several different types of string, made code ugly, added more lines should need , stick characters together. why hard?
solution?
so what's solution? feel should able make function concatenates const char*s handle other object types such std::string, int or double, feel should capable in 1 line, , yet i'm unable find examples of being achieved. should working char* rather constant variant, though i've read should never change value of char* how help?
are there experienced c++ programmers have resolved issue , comfortable c++ strings, solution? there no solution? impossible?
one of simplest solution use c++ empty string. here declare empty string variable named _ , used in front of string concatenation. make sure put in front.
#include <cstdio> #include <string> using namespace std; string _ = ""; int main() { char s[] = "chararray"; string result = _ + "function name = [" + __function__ + "] " "and s [" + s + "]\n"; printf( "%s", result.c_str() ); return 0; } output:
function name = [main] , s [chararray] regarding __function__, found in visual c++ macro while in gcc variable, setreport("failure in " __function__ "; foobar " + foobar + "\n"); work on visual c++. see: https://msdn.microsoft.com/en-us/library/b0084kay.aspx , https://gcc.gnu.org/onlinedocs/gcc/function-names.html
the solution using empty string variable above should work on both visual c++ , gcc.
Comments
Post a Comment