2016-07-10 13 views
2

私は、引数の数を適合させる必要のあるクラスを持っています。メンバstd::functionは、クラスのテンプレートパラメータによって異なります。パラメータは、そのように宣言されていますテンプレートで指定された引数の数

template<char Id, char ... Ids> 
class term; 

その後、いくつかの数値型(同じタイプのすべて)の1 + sizeof...(Ids)引数を取る必要があり、私はstd::functionを持っているクラスのボディに。

体はそうのように宣言されています

template<char Id, char ... Ids> 
class term{ 
    public: 
     template<typename ... Args> 
     void operator()(Args &&... args){ 
      fn(std::forward<Args>(args)...); 
     } 

     std::function<void(/* whats do? */)> fn; 
}; 

私はこれについてどのように行くことができますか?

答えて

2

あなたはパラメータの型がfnのためにあるものを明記していないので、私はすべてのcharを仮定します。その場合:

std::function<void(char, decltype(Ids)...)> fn; 

あなたはパラメータの異なる種類にするために、これを調整することができますが、どのようにあなたがそれを調整することは、署名が正確にどのように見えるかに依存することができます。すべて同じ数値型の場合

、最も簡単な調整は、おそらくこれです:

std::function<void(char, decltype(Ids, YourNumericType{})...)> fn; 
+0

私は質問を更新するつもりはありません。 – CoffeeandCode

+0

@CoffeeandCode、Updatedを更新しました。 – chris

+0

あなたがそれをしている間、私は同じことをどのようにしますか?しかし、関数のパラメータについてはどうしますか? – CoffeeandCode

0

可能なアプローチは、例として、エイリアステンプレートを使用することになります。

template<char...> 
using Arg = int; // or whatever is your type 

// ... 

std::function<void(Arg<>, Arg<Ids>...)> fn; 

または偶数:

template<char> 
using Arg = int; // or whatever is your type 

// ... 

std::function<void(Arg<Id>, Arg<Ids>...)> fn; 

これは最小の実用例に従います。

#include<type_traits> 
#include<functional> 
#include<cassert> 

template<char...> 
using Arg = int; 

template<char Id, char ... Ids> 
class Term { 
public: 
    template<typename ... Args> 
    void operator()(Args &&... args){ 
     fn(std::forward<Args>(args)...); 
    } 

    std::function<void(Arg<>, Arg<Ids>...)> fn; 
}; 

int main() { 
    Term<'a', 'b', 'c'> term; 
    assert((std::is_same<decltype(term.fn), std::function<void(int, int, int)>>::value)); 
} 
関連する問題