dimanche 6 décembre 2020

C++ and ambiguous treatment of string literals

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? Or maybe when and where C++ took a wrong turn? Where is the logical and understandable language that I once loved?

1 commentaire:

  1. Hello everyone, I want to say a special thanks to Dr OGU. for helping me get cured from herpes virus 2019 , I contacted him base on the testimonies I saw about him on the internet I was diagnosed of HERPES Virus i have tried all I can to get cured but all to know avail, until i saw a post in a health forum about a herbal man who prepare herbal medication to cure all kind of diseases including HERPES virus, at first i doubted if it was real but decided to give it a try I was cured by his herbal medicine and natural herbs, kindly contact him today through his email: drogugusolutionhome@gmail.com or text/call: +1 (719) 629 0982

    He’s waiting to help you.


    HE ALSO SPECIALIZE IN THE FOLLOWING Illness;

    If you have any sickness like : H I V/AIDS , CANCER , HERPES 1 or 2 , GENITAL WARTS, Yeast-infection's (HPV), and Hepatitis A, B. Trichomoniasis Gonorrhea, Epilepsy, and Syphilis.

    RépondreSupprimer