supercamel/EmbeddedToolKit

Template restriction due to `==` operator

Closed this issue · 2 comments

T* pt = (T*)space;
auto sz = size();
for(uint32 i = 0; i < sz; i++)
{
if(t == pt[i])
c++;
}

Doesn't that restrict the use of the template to datatypes that can be compared with a == operator? This means that if one need to store eg struct data to the list, they need to actually implement a class and an == operator instead of a simple struct to hold the data.

Hmm, that's an interesting point.
I am pretty sure I have used lists before to store classes without an == operator, but the count function wasn't used, it was ignored by the compiler and there was no error.
Perhaps a function pointer would be a better approach.
What do you think?

I guess that is true. However, since this is still some kind of restriction on the data type I would recommend to keep functions with such requirements in some kind of list_utils.h rather than in the list definition itself.

After all counting occurrences of an object in a list is indeed an extra utility. That's of course only a suggestion. :-)