I have the following code:
#include <cstdlib>
#include <cstdio>
#include <atomic>
enum ATYPE { Undefined = 0, typeA, typeB, typeC };
template<ATYPE TYPE = Undefined>
struct Object
{
Object() { counter++; }
static std::atomic<int> counter;
};
template<ATYPE TYPE>
std::atomic<int> Object<TYPE>::counter(1);
template<ATYPE TYPE>
void test()
{
printf("in test\n");
Object<TYPE> o;
}
int main(int argc, char **argv)
{
test<typeA>();
printf("%d\n", Object<typeA>::counter.load());
Object<typeA>::counter.store(0);
for (int i = 0; i < sizeof(ATYPE); ++i) {
Object<static_cast<ATYPE>(i)>::counter.store(0);
}
return 0;
}
When I compile with the following command line:
clang++ -o test -std=c++11 -stdlib=libc++ test.cpp
I get the following errors:
test.cpp:32:20: error: non-type template argument is not a constant expression
Object<static_cast<ATYPE>(i)>::counter.store(0);
^~~~~~~~~~~~~~~~~~~~~
test.cpp:32:39: note: read of non-const variable 'i' is not allowed in a constant expression
Object<static_cast<ATYPE>(i)>::counter.store(0);
^
testray.cpp:31:18: note: declared here
for (int i = 0; i < sizeof(ATYPE); ++i) {
I understand the problem I believe. The argument of the template needs to be a constexpr and i clearly is not. So the question is, are they possible changes I can do to get this working. By this working, I mean, can I somehow have a better way of resetting these static counters from this template class for each type in ATYPE, other than just doing it manually:
Object<Undefined>::counter.store(0);
Object<typeA>::counter.store(0);
...
Which is not so elegant and practical when ATYPE contains many types.
Thanks a lot for your help and advices.