2016-04-25 6 views
1

テンプレートクラスからtypedefを抽出する方法はありますか?たとえば、これは私がしたいことです:テンプレートクラスからtypedef/usingの定義を抽出する

template<typename T, typename... Args> 
class Foo{ 
public: 
    typedef T(*Functor)(Args...); 
    Foo() = default; 
}; 

template<typename T, typename... Args> 
Foo<T, Args...> make_foo(T(*f)(Args...)){ 
    return Foo<T, Args...>; 
} 

int bar(int i){ 
    return i * 2; 
} 

using type = make_foo(bar)::Functor; 

私はこれを行うことはできません。しかし、私はこれを行うことができます:

using type = Foo<int, int>::Functor; 

この種の私の目的を敗北させる。私は型の形式でそれを抽出することができるように関数をラップする方法はありますか?

答えて

5

decltypeでも十分でしょうか?

using type = decltype(make_foo(bar))::Functor; 
+0

うわー、とても明白な...ありがとう。 – Goodies

4

使用decltype

template<typename T, typename... Args> 
class Foo{ 
public: 
    typedef T(*Functor)(Args...); 
    Foo() = default; 
}; 

template<typename T, typename... Args> 
Foo<T, Args...> make_foo(T(*f)(Args...)){ 
    return Foo<T, Args...>{}; // Small compilation error fixed here. 
} 

int bar(int i){ 
    return i * 2; 
} 

using type = decltype(make_foo(bar))::Functor; 

この演算子は、それが供給され、発現の型を返します。

関連する問題