Home How to determine whether an index of an array of unknown type is empty?
Reply: 0

How to determine whether an index of an array of unknown type is empty?

user10682 Published in September 21, 2018, 8:04 am
template<typename K, typename V, int N>
    class KVList
        int m_size;
        K m_key[N] = {};
        V m_value[N] = {};

        KVList& add(const K&, const V&)
             //Check if index is empty or null
                    //Add key value pair

Hey guys,

I'm creating a templated class that has 3 template parameters. My question, as stated above, is how I can determine whether or not an array at certain indexes is NULL or has no user-defined value.

Currently passing these datatypes as template arguments:

w4::KVList<std::string, double, 5> x;
w4::KVList<std::string, std::string, 5> y;

I was thinking maybe something along the lines of type conversions to bool might help but I'm stuck.

How can I check if the index has a null value for occurrences KVList accepts different datatypes or classes than the ones specified above?

share|improve this question
  • Array elements are never "empty". They will always have a value. And you initialize your arrays, so depending on the types, they will have default-constructed object or zero-initialized values. – Some programmer dude Feb 11 at 19:16
  • There is no such thing as a "null" or "empty" double or int. – Mat Feb 11 at 19:16
  • Why are you asking about array when you clearly have Key and Value pairs? – Killzone Kid Feb 11 at 19:17
  • 2
    @Schwiftyy Can you elaborate at what you actually want to solve with that code? There might be alternatives you might not have thought of. E.g. std::optional might come in handy. – user9212993 Feb 11 at 19:17
  • @TheDude It's supposed to be a parallel array that uses N to declare size of the array and I guess iterate through it. K and V to be used like a dictionary. Let me rephrase it better, I want to know whether or not the array index is not user-defined so I can add values at the index. – Schwiftyy Feb 11 at 19:22

1 Answer 1

active oldest votes
up vote 1 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.518846 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO