2

I have a case where I have to set std::string to NULL. The code is like this: const std::string variable = "HOME"; const std::string str = getenv(variable.c_str());

Problem is, getenv() returns NULL if it does not find the environment variable and since we can't set std::string to NULL value, we have to assign it an empty string.

One option is: const std::string str = (getenv(variable.c_str()) == NULL ? "" : getenv(variable.c_str())); Only that it calls getenv() twice.

What is the best practice to achieve this result?

6
  • Why do you need the second getenv call? I'm not sure I follow that. It seems like the first one by itself achieves what your first two lines of code did.
    – wolfPack88
    Jun 12, 2014 at 13:16
  • @wolfPack88: To have something on the right side of the ternary statement.
    – Matt Kline
    Jun 12, 2014 at 13:16
  • Ah... read it as two lines, wasn't paying attention. My bad
    – wolfPack88
    Jun 12, 2014 at 13:17
  • By the way, it isn't called twice.
    – chris
    Jun 12, 2014 at 13:18
  • 1
    @chris I think this case is something like this ideone.com/4LhR2B
    – tnkousik
    Jun 12, 2014 at 13:30

3 Answers 3

5
std::string string_from_char_array(const char* str)
{
  return str ? str : "";
}

Then call it like

const std::string str = string_from_char_array(getenv(variable.c_str()));
5
  • To be a little more clear about what's going on, might it be better to do return str != null ? str : "";? I know it's a style issue.
    – Matt Kline
    Jun 12, 2014 at 13:17
  • 2
    @MattKline, Personally, pointers as booleans are one of the few things I think make perfect sense.
    – chris
    Jun 12, 2014 at 13:19
  • @Matt I guess it is a style issue, but then I'm on the side that thinks that comparing a pointer against NULL (or std::nullptr) is needlessly verbose. ;)
    – D Drmmr
    Jun 12, 2014 at 13:20
  • With lambdas you can even do it in one line: const std::string s = [](char const* str) { return str ? str : ""; }(getenv(v.c_str())); Jun 12, 2014 at 13:47
  • @ComicSansMS Yes, but then you need to write the same thing each time (though it's a simple thing). And I'd say it doesn't improve readability.
    – D Drmmr
    Jun 12, 2014 at 18:47
3

What is the best practice to achieve this result?

Roll your own (wrap it):

std::string safe_getenv(const std::string& var)
{
    auto env = getenv(var.c_str());
    return env ? std::string{env} : std::string{};
}

auto result = safe_getenv("blah");
1
  • If doing this, I'd probably make it a bit more generic. Something like T value_or(const T &t, const T &def) {return t ? t : def;}, but maybe with a performance tweak or two unless it's inlined as is.
    – chris
    Jun 12, 2014 at 13:46
0

Only that it calls getenv() twice.

What is the best practice to achieve this result?

You could avoid calling it twice by assigning the result of getenv() to an auto variable like this


    auto var = getenv(variable.c_str();
    const std::string str = (var == NULL ? "" : var);
 
Hope it helps you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.