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

前端 未结 16 1569
有刺的猬
有刺的猬 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 01:03

    A usage is in TensorFlow's REGISTER_KERNEL_BUILDER macro. Each TensorFlow Op could have one or more kernels as its implementations. These kernels are registered with a registrar. The registration of a kernel is done by defining a global variable -- the constructor of the variable can do the registration. Here the authors use __COUNTER__ to give each global variable a unique name.

    #define REGISTER_KERNEL_BUILDER(kernel_builder, ...) \
      REGISTER_KERNEL_BUILDER_UNIQ_HELPER(__COUNTER__, kernel_builder, __VA_ARGS__)
    
    #define REGISTER_KERNEL_BUILDER_UNIQ_HELPER(ctr, kernel_builder, ...) \
      REGISTER_KERNEL_BUILDER_UNIQ(ctr, kernel_builder, __VA_ARGS__)
    
    #define REGISTER_KERNEL_BUILDER_UNIQ(ctr, kernel_builder, ...)          \
      static ::tensorflow::kernel_factory::OpKernelRegistrar                \
      registrar__body__##ctr##__object(                                 \
          SHOULD_REGISTER_OP_KERNEL(#__VA_ARGS__)                       \
          ? ::tensorflow::register_kernel::kernel_builder.Build()   \
          : nullptr,                                                \
          #__VA_ARGS__, [](::tensorflow::OpKernelConstruction* context) \
                -> ::tensorflow::OpKernel* {                \
                  return new __VA_ARGS__(context);          \
                });
    

提交回复
热议问题