2011-12-25 14 views
-1

私はこのようにいろいろ書いをしたい。すなわちは、テンプレートパラメータにクラスメソッドを指定します - 必要に応じて(デフォルトパラメータ)を

template <class T,class t_function = t::default_function> 
class foo 
{ 
    public: 
    static void bar(T t) 
    { 
     t.t_function(); 
    } 
}; 

class example1 
{ 
    public: 
     void default_function() 
     { 
      std::cout<<"test"<<std::endl; 
     } 
}; 

class example2 
{ 
    public: 
     void someotherfunction() 
     { 
     std::cout<<"test2"<<std::endl; 
     } 
}; 

//how to use it 

foo<example1>::bar(example1()); //should work, since example1 has the default function 

foo<example2,example2::someotherfunction>::bar(example2()); //should work too 

:私は、その関数のユーザーは選択肢を提供できるようにしたいのですがデフォルトのメソッドの代わりに実行されるメソッドです。

私はC++でこれを達成できますか?

+0

-1と説明なし。悲しい顔 :( – TravisG

答えて

1

たぶん、このような何か:

template <class T, void(T::*TFun)() = &T::default_function> 
struct Foo 
{ 
    static void bar(T t) 
    { 
     (t.*TFun)(); 
    } 
}; 

使用方法:すでに

struct Zoo { void default_function() { } }; 
struct Zar { void bla() { } }; 

int main() 
{ 
    Zoo z1; 
    Zar z2; 
    Foo<Zoo>::bar(z1); 
    Foo<Zar, &Zar::bla>::bar(z2); 
} 
関連する問題