0

On C++ string can expression two case :

1. string flag;                     /* empty string */
1. string flag = "Other content";   /* other string */

On Java String can expression three case :

1. String flag = NULL;              /* NULL */
2. String flag = "";                /* empty string */
2. String flag = "Other content";   /* other string */

On C++ can we expression the string as NULL (nullptr) ? if not use pointer .

My scene: I got string data from mysql , mysql expression way same as Java for string , NULL/empty/other .

My data model is used for Mysql And nlohmann Json .

3
  • 3
    If you really need to distinguish between "the absence of a string" and "an empty string", you could use std::optional Commented Sep 27, 2022 at 3:35
  • 1
    In java, everything is an object which in turn essentially points to the memory representing the object. You can use a ref counting pointer to the string to get the same computational result. You could also make a string of more than one character where the first character is null... Commented Sep 27, 2022 at 3:37
  • Thanks , std::optional is well pointer is whell , but use these way , I must to Self-compatible with nlohmann Json . maybe is my only way. Commented Sep 27, 2022 at 3:46

3 Answers 3

2

So you can use std::optional to express this:

std::optional<std::string> null_string = std::nullopt; // this is the "null" state

if (optional_string) { /// Can be used like a null or bool type

std::optional<std::string> empty_string = ""; // This is the empty string
std::optional<std::string> other_string = "Other content"; // The full string

Just be aware, that to use string interfaces, you will need to get the value of the string with .value() or operator*:

void print_string(const std::string& s);

print_string(*other_string);

This requires you to check that the string is actually valid before calling or you will invoke undefined behaviour.

Sign up to request clarification or add additional context in comments.

Comments

1

The answer below is quite theoretical.
In practice usually there's no real need to distinguish between an empty string and a null one and using std::string as is is the best solution. And if you only need to add only the "nullabilty" to your C++ string you can use std::optional as mentioned in the comments and other answer.


Having said that:

In Java a String is a reference type, meaning variables like flag actually hold a reference to the String object (and the reference can be null). An object can have multiple references to it, and when this number becomes zero the object can be automatically destroyed.

If you want to achieve an overall similar behavior in C++, you can use std::shared_ptr holding a std::string:

#include <memory>
#include <string>
//...
std::shared_ptr<std::string> s1 = nullptr;      // null string
std::shared_ptr<std::string> s2 = std::make_shared<std::string>("");    // empty string
std::shared_ptr<std::string> s3 = std::make_shared<std::string>("abs"); // non empty string

2 Comments

but you shouldn't right? just use value semantics
@FantasticMrFox Added a preface about whether this construct is actually needed.
0

On C++ can we expression the string as NULL (nullptr) ?

No you can't . want to expression string as nullptr need string pointer

On C++ can we expression the string as NULL (nullptr) ? if not use pointer .

Maybe std::optional is answer .

My data model is used for Mysql And nlohmann Json .

  1. assume select std::optional<string> flag

  2. for mysql , you can expression string NULL , flag = nullopt

  3. for nlohmann Json , you must to compatible with std::optional

    namespace nlohmann {
      /* https://github.com/nlohmann/json#how-do-i-convert-third-party-types */
      template <typename T>
      struct adl_serializer<std::optional<T>> {
          static void to_json(json& j, const std::optional<T>& opt) {
            if(opt.has_value()) {
                // this will call adl_serializer<T>::to_json which will
                // find the free function to_json in T's namespace!
                j = opt.value();
            } else {
                j = nullptr;
            }
          }
    
          static void from_json(const json& j, std::optional<T>& opt) {
              if (j.is_null()) {
                opt = std::nullopt;
              } else {
                opt = j.get<T>(); // same as above, but with
                                  // adl_serializer<T>::from_json
              }
          }
      };
    }
    

Other things

  1. Copy form @wohlstad In Java a String is a reference type, meaning variables like flag actually hold a reference to the String object (and the reference can be null).

My Reference :

  1. https://github.com/nlohmann/json/issues/1749
  2. https://www.kdab.com/jsonify-with-nlohmann-json/

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.