5

我有以下代码:

#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;
}

当我使用以下命令行编译时:

clang++ -o test -std=c++11 -stdlib=libc++ test.cpp

我收到以下错误:

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) {

我理解我相信的问题。模板的参数需要是 constexpr 而 i 显然不是。所以问题是,我是否可以做一些改变来让它发挥作用。通过这种工作,我的意思是,除了手动执行之外,我能否以某种更好的方式从该模板类中为 ATYPE 中的每种类型重置这些静态计数器:

Object<Undefined>::counter.store(0);
Object<typeA>::counter.store(0);
...

当 ATYPE 包含许多类型时,这不是那么优雅和实用。

非常感谢您的帮助和建议。

4

1 回答 1

7

对于这类事情,递归通常是一个简单的解决方案:

#include <type_traits>

enum ATYPE { Undefined = 0, typeA, typeB, typeC, ATYPE_END };

void reset_Object_counter(std::integral_constant<ATYPE, ATYPE_END>)
{}

template < ATYPE n = Undefined >
void reset_Object_counter(std::integral_constant<ATYPE, n> p = {})
{
    Object<p>::counter.store(0);
    reset_Object_counter(std::integral_constant<ATYPE,
                                                static_cast<ATYPE>(n+1)>{});
}

对于这种情况,AFAIK,函数模板专业化也可以工作(而不是第一次重载):

template<>
void reset_Object_counter<ENUM_END>(std::integral_constant<ATYPE, ENUM_END>)
{}

无论哪种方式,用法只是reset_Object_counter();将所有Object<...>的计数器设置为0.


这里的integral_constant解决方案实际上有点矫枉过正,对于这个问题,一个非类型模板参数就足够了(因为函数模板特化可以代替结束递归的重载)。

template < ATYPE n = Undefined >
void reset_Object_counter()
{
    Object<n>::counter.store(0);
    reset_Object_counter<static_cast<ATYPE>(n+1)>();
}
template<>
void reset_Object_counter<ENUM_END>()
{}
于 2013-08-11T23:47:31.823 回答