具有范围限定符的模板语法的含义

Posted

技术标签:

【中文标题】具有范围限定符的模板语法的含义【英文标题】:Meaning of template syntax with scope qualifier 【发布时间】:2013-03-25 19:28:10 【问题描述】:

我最近看到了这个:

template <class U> struct ST

...
;
template <class U, class V>
struct ST<U V::*>

...
;

我假设第二个模板是第一个模板的特化。

但是U V::* 的语义是什么???

【问题讨论】:

【参考方案1】:

这意味着“指向类 V 的成员的指针,其中成员的类型是 U”。例如,

struct X

    int x = 0;
;

// ...

int X::*p = &X::x;     // <== Declares p as pointer-to-member

ST<decltype(&X::x)> s; // <== Will instantiate your template specialization,
                       //     with U = int and V = X

ST<int X::*> t;        // <== Will instantiate your template specialization,
                       //     with U = int and V = X

【讨论】:

以上是关于具有范围限定符的模板语法的含义的主要内容,如果未能解决你的问题,请参考以下文章