chrck if item is NULL with template

Hi

I want to know if my var is NULL or not.
the var can be string or integer.

this code is fine to int but error to string
if (key==NULL)
what can I do?
What is a NULL string? It makes no sense. It only makes sense for an int if you decide that NULL is zero.
Note that NULL is the same as 0 so if key is an int there is no way tell the the difference between key having value 0 or NULL.

Null is normally used with pointers. Maybe you want to make key a pointer?
so for string I've to write
if (key="")
but how can I recognize if it's int or string ?
Last edited on
you don't need to:
1
2
if (key==K())
  ...
thank u

and how to recognize if type is string ?
(while it's not NULL or empty)
I want to use function only if type is string
Last edited on
Then you have to use template specialization: http://www.learncpp.com/cpp-tutorial/144-expression-parameters-and-template-specialization/ under Template specialization.
if RTTI is on you can check the type with: typeid(K) == typeid(string)
I haven't check it, but it should work

http://www.cplusplus.com/reference/std/typeinfo/type_info/
I tried the typeid but I'm getting this error:

Error 1 error C2440: 'type cast' : cannot convert from 'std::string' to '__int64'


1
2
3
4
if (typeid(K) == typeid(string))
			innerKey=stringToNumber(_key);
		else
			innerKey=(long long int)_key;
Your problem is the time of evaluation. line 1 is evalutated at runtime and so is line 4 which leads to the compiler error.

what you need is evaluation at compile time. i.e. that the compiler doesn't see line 4 or 2 depending on the type. For this check out the boost enable_if:

http://www.boost.org/doc/libs/1_52_0/libs/utility/enable_if.html
I doesn't success to implement it.
can you give me example on my above code ?
Last edited on
can you give me example on my above code ?
Sorry, I never dealt with that, so no.

What you can do is providing a second template parameter which determines the conversion function to innerKey
Topic archived. No new replies allowed.