C++ Partial Specialization ( Function Pointers )

2019-06-02 15:38发布

Can any one please tell, whether below is legal c++ or not ?

template < typename s , s & (*fn) ( s * ) > 
class c {};

// partial specialization

template < typename s , s & (*fn) ( s * ) > 
class c < s*, s* & (*fn)(s**)  {};

g++ ( 4.2.4) error: a function call cannot appear in a constant-expression error: template argument 2 is invalid

Although it does work for explicit specialization

int & func ( int * ) { return 0; }
template <> class c < int , func> class c {};

1条回答
姐就是有狂的资本
2楼-- · 2019-06-02 16:35

I think you mean

template < typename s , s & (*fn) ( s * ) > 
class c {};

// partial specialization
template < typename s , s & (*fn) ( s * ) > 
class c < s*, fn >  {};
查看更多
登录 后发表回答