2016-12-31 64 views
1

指向成员指针作为非类型模板参数的用例是什么?实际使用指向成员非类型模板参数的指针

例如:

class X { 
public: 
    int n; 
}; 


template <typename T, T nontype_param> 
class C 
{ 
public: 
    void doSomething() 
    { 
     //what goes here to access or use nontype_param? 
    } 
}; 

void test() 
{ 
    C<int X::*, &X::n> c; 
    c.doSomething(); 
} 
+0

也许与POD结构一起使用?没有一定的背景,确实很难说出任何事情。 –

+2

类似'X x; x。* nontype_param = 42;'? – Jarod42

+0

@Someprogrammerdude - 正是我在阅读模板时遇到的情况。真的没有比这更多的上下文,因此关于用例的问题:) – tomatoRadar

回答

0

行情从Bjarne的书:

A型模板参数可以被用来作为一种后来在模板参数列表。例如:

template<typename T, T default_value> 
class Vec { 
// ... 
}; 

Vec<int,42> c1; 
Vec<string,""> c2; 

当与默认模板参数(第25.2.5节)结合使用时,这变得特别有用;对于 示例:

template<typename T, T default_value = T{}> 
class Vec { 
    // ... 
}; 

Vec<int,42> c1; 
Vec<int> c11; // default_value is int{}, that is, 0 

Vec<string,"for tytwo"> c2; 
Vec<string> c22; // default_value is string{}; that is, "" 
+0

指向成员非类型模板参数__ – Danh

相关问题