C++ 从模板类中提取typedef/using定义
有没有办法从模板类中提取typedef?例如,这就是我想做的:C++ 从模板类中提取typedef/using定义,c++,templates,types,casting,C++,Templates,Types,Casting,有没有办法从模板类中提取typedef?例如,这就是我想做的: 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...)){
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;
模板
福班{
公众:
typedef T(*函子)(参数…);
Foo()=默认值;
};
样板
Foo make_Foo(T(*f)(Args…){
返回Foo;
}
整型条(整型i){
返回i*2;
}
使用类型=make_foo(bar)::函子;
我不能这样做。但是,我可以这样做:
using type = Foo<int, int>::Functor;
使用type=Foo::Functor;
对我来说,这样做是不符合目的的。有没有办法包装一个函数,这样我就可以以类型形式提取它?就足够了
using type = decltype(make_foo(bar))::Functor;
那就好了
using type = decltype(make_foo(bar))::Functor;
使用
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;
模板
福班{
公众:
typedef T(*函子)(参数…);
Foo()=默认值;
};
样板
Foo make_Foo(T(*f)(Args…){
return Foo{};//此处修复了小编译错误。
}
整型条(整型i){
返回i*2;
}
使用type=decltype(make_foo(bar))::Functor;
此运算符返回其输入的表达式的类型。使用
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;
模板
福班{
公众:
typedef T(*函子)(参数…);
Foo()=默认值;
};
样板
Foo make_Foo(T(*f)(Args…){
return Foo{};//此处修复了小编译错误。
}
整型条(整型i){
返回i*2;
}
使用type=decltype(make_foo(bar))::Functor;
这个操作符返回它所输入的表达式的类型。Wow,太明显了。。。谢谢。哇,太明显了。。。谢谢