0

In c++, as far as I know, I believe that a std::string cannot represent the absence of a value. Therefore, in case of a method returning a pointer to a std::string such as, std::string *myMethod(). Does it make sense to check if the return value from myMethod is NULL?

The use case related to my question is the following: the std::string *method() was automatically created by gsoap stub library from a WSDL file. I do not have access to the WSDL file, hence i cannot change the signature of the method. My c++ application is the client and the server is a java application. Both of these applications communicate via web services

vilanova
  • 77
  • 1
  • 10
  • 2
    Yes, because `std::string*` is a pointer to a string, not a `std::string`; pointers can be `nullptr`. – bku_drytt Sep 27 '15 at 16:08
  • Try nullptr If the function returns a pointer then a test may be required – Ed Heal Sep 27 '15 at 16:09
  • 3
    Is there really a need to differ between absence of a string, and an empty string? What is your use-case? – Some programmer dude Sep 27 '15 at 16:14
  • 1
    You could also do something similar to [`std::map::insert`](http://en.cppreference.com/w/cpp/container/map/insert), which can return a [`std::pair`](http://en.cppreference.com/w/cpp/utility/pair). You could modify the function to return a `std::pair` where the `second` member of the pair is saying if the string "exists" or not. – Some programmer dude Sep 27 '15 at 16:22
  • The use case is the following: the std::string *method() was automatically created by gsoap stub library from a WSDL file. I do not have access to the WSDL file, hence i cannot change the signature of the method. My c++ application is the client and the server is a java application. Both of these applications communicate via web services. – vilanova Sep 27 '15 at 16:29
  • 3
    Returning a pointer in this case is almost certainly a terrible idea. Use something like Boost.Optional … – Konrad Rudolph Sep 27 '15 at 16:29

3 Answers3

1

Yes, it makes sense.

The return type is a pointer of type string..pointers contain addresses, and it is possible that it may be null.

In this case, returning a pointer and null check is the right way to go about it.

If you don't want to check for null, then the alternative would be return by reference, but the usage is complex and is best fit when used for operator chaining.Whether that fits the current use case is another question by itself.

Is the practice of returning a C++ reference variable, evil?

Community
  • 1
  • 1
basav
  • 1,475
  • 12
  • 20
  • 1
    Where does reference come in – Ed Heal Sep 27 '15 at 16:14
  • "If you donot want to check for null"....it is an alternative if he is sure that there will be no such use case with his current signature... – basav Sep 27 '15 at 16:16
  • how do you decide if you return a reference or return a pointer and what are the tradeoffs?? that link makes it clear. – basav Sep 27 '15 at 16:22
  • @basav "If you don't want to check for null then return by reference" – no, why? Most probably you **don't** want to return a reference, you want to just return a value. (Looks like OP is trying to *generate* some sort of string on the fly, not necessarily storing it in the object; and returning a reference to locals doesn't do any good.) – The Paramagnetic Croissant Sep 27 '15 at 16:35
  • changed the answer..just suggesting an alternate way, not necessarily the correct way. in this scenario..consider both the cases and choose what is best.. – basav Sep 27 '15 at 16:39
1

Yes, it makes sense. The relevant part is hidden in a comment: "method() was automatically created ... from a WSDL file". In text-based protocols such as WDSL that follow a "key-value" pattern, the absence of a key is syntactically different from a key="" empty key. A generic library cannot assume the two are semantically equivalent. Thus it will use return nullptr to signal the absence of a key.

MSalters
  • 173,980
  • 10
  • 155
  • 350
0

If your function returns a std::string*, you can check if it returns NULL (or better, nullptr) because you don't check the string itself but a pointer to the string.

blakelead
  • 1,780
  • 2
  • 17
  • 28