This a scratching my head question, but it works, with const, but not constexpr and I'm wondering if you smart folks can explain. Compiled with g++ -std=c++14.
struct Service
{
std::string name;
enum {
thread,
interrupt,
end_types
} que_type;
};
const Service namedServices[] =
{
{"abc", Service::thread},
{"efg", Service::thread},
{"hij", Service::interrupt},
{"klm", Service::thread},
{"nop", Service::interrupt},
{"qrs", Service::thread},
{"", Service::end_types}
};
constexpr int thcnt()
{
int cnt = 0;
for (const Service* sp = namedServices; sp->que_type != Service::end_types; sp++) {
if (sp->que_type == Service::thread)
cnt++;
}
return cnt;
}
int main(int argc, char** argv)
{
std::string strs[thcnt()];
...
This compiles and as far as I can tell does the right thing. If I change to
constexpr Service namedServices[] =
I get
... error: the type 'const Service []' of constexpr variable 'namedServices' is not literal
};
^
This seems weird, but perhaps there is a good reason. Or maybe, it will be fixed in c++20.
thanks