Has anyone ever had a use for the __COUNTER__ pre-processor macro?

前端 未结 16 1521
有刺的猬
有刺的猬 2020-11-29 00:37

The __COUNTER__ symbol is provided by VC++ and GCC, and gives an increasing non-negative integral value each time it is used.

I\'m interested to learn w

16条回答
  •  清酒与你
    2020-11-29 00:46

    It's used in ClickHouse's metrics system.

    namespace CurrentMetrics
    {
        #define M(NAME) extern const Metric NAME = __COUNTER__;
            APPLY_FOR_METRICS(M)
        #undef M
        constexpr Metric END = __COUNTER__;
    
        std::atomic values[END] {};    /// Global variable, initialized by zeros.
    
        const char * getDescription(Metric event)
        {
            static const char * descriptions[] =
            {
            #define M(NAME) #NAME,
                APPLY_FOR_METRICS(M)
            #undef M
            };
    
            return descriptions[event];
        }
    
        Metric end() { return END; }
    }
    

提交回复
热议问题