c++ 变量类型判断

  1. 请问下面这个变量m是什么类型?NOTIFICATION 是一个 struct

class EventSensor;

EventSensor<NOTIFICATION> m;
阅读 3k
评论
    3 个回答
    • 1.8k

    如果EventSensor是一个类模板(你给出的声明不是类模板的声明)。变量声明EventSensor<NOTIFICATION> m;会声明一个以NOTIFICATION为模板参数特化出的类的实例m,这个类称作"EventSensor<NOTIFICATION>"(c++标准中便是如此称呼的)。

    也就是说,变量m的类型就是EventSensor<NOTIFICATION>

    When template arguments are provided or, for function and class (since C++17) templates only, deduced, they are substituted for the template parameters to obtain a specialization of the template, that is, a specific type or a specific function lvalue.

    引自cppreference, template