I like writing checks for a function over a list. For this I usually write a function like this:
inline bool good_strings(const std::vector<const char *> & items)
{
for (i in items) {
if (not is_good(i)) return false;
}
return true;
}
Then I can write like if (all_good({"a", "b", "c", "d", "e"})) {...}
and it looks really nice. This is good to use when your check for a couple of items grows bigger like this:
if (is_good("a") and is_good("b") and /* that's too much, man */ is_good("c")) {...}
But I'm concerned with the overhead of the container I'm using, and also it's hard to choose one: std::vector
, std::list
, QList
, QStringList
or maybe even std::array
or std::initializer_list
- which should be used for inline functions? And which of these a has minimum or even zero overhead when creating using the {}
brackets?
Alright, and update: I grabbed my friend licensed IDA Pro and checked some options.
std::initializer_list
: the function doesn't even inline, and there is overhead for creating the list and copying pointers.std::vector
: the function does inline, however, there is an overhead for creating a vector and copying pointers there.std::array
: not as good-looking because of template specialization, and the function doesn't inline. So calling it many times creates many similar chunks of code. However, there is no overhead for array creation, and all pointers are passed as function parameters, which is fast forx86_64
register calling a convention.
The question remains, is there an absolutely zero-cost container?
constexpr
context? If you can perform this test at compile time overhead doesn't really matter. – Abubekr