In my C++/Linux application I want to create a temp folder.
The code is very simple:
std::string tempFolder(mkdtemp("foo"));
To my surprise I received a warning message:
warning: ISO C++11 does not allow conversion from string literal to 'char *'
Ok, as I remember C++ treats string literals as std::string
, not a char*
. I also know that I can avoid this message by declaring char *
as const char*
. So my code might look like the following:
const char *tpl = "foo";
std::string tempFolder(mkdtemp(tpl));
if not for the fact that mkdtemp
requires char *
and not const char*
. So explicit casting required here and now my code should look like that:
const char *tpl = "foo";
std::string tempFolder(mkdtemp(const_cast<char *>(tpl)));
In my opinion that looks absolutely ridiculous, the code looks absolutely incomprehensible and overloaded.
So my question - what am I doing wrong?