0

I have a method with a template parameter

bool insert_into_tree(const T new_key, const long &new_index){}

T is limited to std::string and int types. I would like values of new_key to be meaningful and thus I do not want an empty std::string but can accept a zero value for int.

This is an example of a Java template comparison to null

if (key == null) {}

key defined as T extends Comparable<? super T>

Within c++

key is defined as template <class T>

If T is std::string, there is no overloaded operator== such that comparison to NULL is possible.

If T is int, comparison to null is acceptable.

Thus, what method could I employ to make the template parameter comparison work as in Java?

In other words, if in Java I have a template parameter I can compare to null, how could I compare a template parameter in c++ such that NULL or some other appropriate value indicating null could be achieved.

6
  • 1
    Semantically, what would you consider to be the "null" value of bool? Commented Apr 12, 2013 at 13:08
  • 1
    What are you trying to achieve? Commented Apr 12, 2013 at 13:08
  • Comparing int to NULL works, but probably not the way you think. It simply compares to 0 (since NULL is a macro for 0). In the Java case, key is a pointer (which can thus be null). In your C++ case, you're working with a key object directly, so being null is not really defined there. Commented Apr 12, 2013 at 13:09
  • @NPE False as NULL is a #define equating to the numerical value 0 Commented Apr 12, 2013 at 13:20
  • @mfontanini Reading comprehension ... Thus, what method could I employ to make the template parameter comparison work as in Java? Commented Apr 12, 2013 at 13:23

2 Answers 2

2

First remember that this isn't Java, it's C++ and they work different ways. Possibly the most similar way to do this would be to create a Comparable template and ensure that both key and the classes you want to compare to instantiate it. So instead of using std::string you'd use Comparable< std::string > That's one way of doing it but almost certainly not the best way.

A better way overload the comparison operator to provide the comparisons that you want. That would allow you to be specific in your comparison. Watch out for implicit casting though.

Whichever way you chose will be largely based on the broader context of your code and without more information, it's hard to be specific about which method is best.

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

Comments

2

C++ has no nullable types (all types in C++ are by default value-types, there is no reference-types, how in Java, so you cannot compare some type with some magic null). You can use pointers, or boost::optional here.

If T is int, comparison to null is acceptable.

Yes, it's acceptable, since NULL in C++ is simply 0.

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.