gpt4 book ai didi

c++ - 从模板类中提取 typedef/using 定义

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:25:24 24 4
gpt4 key购买 nike

有没有办法从模板类中提取 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...)){
return Foo<T, Args...>;
}

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

using type = make_foo(bar)::Functor;

我做不到。但是,我可以这样做:

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

这种做法违背了我的目的。有没有什么方法可以包装一个函数,以便我可以以类型形式提取它?

最佳答案

decltype够好吗?

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

关于c++ - 从模板类中提取 typedef/using 定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36848644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com