C++ 指向任意类方法的模板非类型指针
假设我有:C++ 指向任意类方法的模板非类型指针,c++,templates,c++11,C++,Templates,C++11,假设我有: struct Foo { void a(); void b(const int& ); int c(); }; 我可以创建一个函数,将指向-Foo方法的任意指针作为参数: template <typename R, typename... Formal, typename... Args> R call(Foo* f, R (Foo::*method)(Formal...), Args&&... args) { r
struct Foo {
void a();
void b(const int& );
int c();
};
我可以创建一个函数,将指向-Foo
方法的任意指针作为参数:
template <typename R, typename... Formal, typename... Args>
R call(Foo* f, R (Foo::*method)(Formal...), Args&&... args) {
return (f->*method)(std::forward<Args>(args)...);
}
int gratuitous = call(&some_foo, &Foo::c);
但是,有没有一种方法可以创建一个可以在任何指向类方法的指针上作为模板的函数?我希望能够做到:
works_for_anything<&Foo::a>(&some_foo);
works_for_anything<&Foo::b>(&some_foo, 42);
int result = works_for_anything<&Foo::c>(&some_foo);
适用于任何东西(一些东西);
为任何事情工作(42岁);
int result=works\u for\u anything(&some\u foo);
这对你有用吗
template< typename T, T >
class works_for_anything_t;
template< typename R, typename... Args, R (*f)(Args...) >
class works_for_anything_t< R (*)(Args...), f >{
public:
R operator()( Args... args ){ return f(args...); }
};
template< typename T, typename R, typename... Args, R (T::*f)(Args...) >
class works_for_anything_t< R (T::*)(Args...), f >{
public:
R operator()( T& v, Args... args ) { return (v.*f)(args...); }
works_for_anything_t(T& v)
: v_(v) { }
private:
T& v_;
};
template< typename T, typename R, typename... Args, R (T::*f)(Args...) const >
class works_for_anything_t< R (T::*)(Args...) const, f >{
public:
R operator()( const T& v, Args... args ) const { return (v.*f)(args...); }
works_for_anything_t(const T& v)
: v_(v) { }
private:
const T& v_;
};
#define works_for_anything(f) works_for_anything_t<decltype(&f), &f>
struct Foo {
void a();
void b(const int& );
int c();
};
int test();
int main() {
Foo foo;
works_for_anything(Foo::b){foo}( 42 );
works_for_anything(test){}();
return 0;
}
模板
班级为任何事情而工作;
模板
类为任何对象工作(Args…,f>{
公众:
R运算符()(Args…Args){返回f(Args…;}
};
模板
类为任何对象工作\u t{
公众:
R运算符()(T&v,Args…Args){return(v.*f)(Args…;}
为任何事物工作(t&v)
:v_uv(v){}
私人:
T&v;
};
模板
类为任何对象工作\u t{
公众:
R运算符()(常量T&v,Args…Args)常量{return(v.*f)(Args…;}
为任何事情工作(施工测试与验证)
:v_uv(v){}
私人:
康斯特T&v;
};
#为任何东西定义工作(f)为任何东西定义工作
结构Foo{
使a()无效;
无效b(常数int&);
int c();
};
int测试();
int main(){
富富,;
为任何事情工作(Foo::b){Foo}(42);
为任何东西工作(测试){}();
返回0;
}
否,非类型模板参数必须具有固定类型。为什么要这样做?关于template
@ernon,我想你的意思是template
,但是调用者必须指定类型,例如几乎可以工作(&some\u foo,42)代码>。但这似乎是多余的。也许用C++1Z:模板呃,使用typename
,谁会喜欢呢?那些关键词已经被超额订阅了。为什么不使用模板
?
template< typename T, T >
class works_for_anything_t;
template< typename R, typename... Args, R (*f)(Args...) >
class works_for_anything_t< R (*)(Args...), f >{
public:
R operator()( Args... args ){ return f(args...); }
};
template< typename T, typename R, typename... Args, R (T::*f)(Args...) >
class works_for_anything_t< R (T::*)(Args...), f >{
public:
R operator()( T& v, Args... args ) { return (v.*f)(args...); }
works_for_anything_t(T& v)
: v_(v) { }
private:
T& v_;
};
template< typename T, typename R, typename... Args, R (T::*f)(Args...) const >
class works_for_anything_t< R (T::*)(Args...) const, f >{
public:
R operator()( const T& v, Args... args ) const { return (v.*f)(args...); }
works_for_anything_t(const T& v)
: v_(v) { }
private:
const T& v_;
};
#define works_for_anything(f) works_for_anything_t<decltype(&f), &f>
struct Foo {
void a();
void b(const int& );
int c();
};
int test();
int main() {
Foo foo;
works_for_anything(Foo::b){foo}( 42 );
works_for_anything(test){}();
return 0;
}