C++ 如何推导C++;返回模板中的类型?
我有一个函子,我希望返回类型自动推断。我该怎么做C++ 如何推导C++;返回模板中的类型?,c++,templates,functor,generic-programming,c++03,C++,Templates,Functor,Generic Programming,C++03,我有一个函子,我希望返回类型自动推断。我该怎么做 template <typename _ScalarL, typename _ScalarR> struct Multi { DEDUCEDTYPE operator()(_ScalarL input1, _ScalarR input2) const { return input1 * input2; } }; int main(){ Multi<int, double>
template <typename _ScalarL, typename _ScalarR>
struct Multi
{
DEDUCEDTYPE operator()(_ScalarL input1, _ScalarR input2) const
{
return input1 * input2;
}
};
int main(){
Multi<int, double> mt;
mt(1,2.0);
}
模板
结构多
{
推断类型运算符()(\u ScalarL input1,\u ScalarR input2)常量
{
返回input1*input2;
}
};
int main(){
多机器翻译;
mt(1,2.0);
}
如何自动获取演绎类型?我相信在c++11之前,你注定要手工提供演绎。。。您可以为以下对象创建具有专门化的帮助器结构:
template <typename A, typename B>
struct deduce { };
template <>
struct deduce<int, double> {
typedef double type;
};
template <typename ScalarL, typename ScalarR>
struct Multi
{
typename deduce<ScalarL, ScalarR>::type operator()(ScalarL input1, ScalarR input2) const
{
return input1 * input2;
}
};
int main(){
Multi<int, double> mt;
mt(1,2.0);
}
模板
结构演绎{};
模板
结构演绎{
typedef双字型;
};
模板
结构多
{
typename推断::类型运算符()(ScalarL input1,ScalarR input2)常量
{
返回input1*input2;
}
};
int main(){
多机器翻译;
mt(1,2.0);
}
编辑:
但是,更通用的方法是创建类型的优先级层次结构,在推断结果类型时应考虑这些优先级。示例代码:
#include <iostream>
template <typename T>
struct MoreGeneralNumber { };
template <>
struct MoreGeneralNumber<long>: MoreGeneralNumber<int> {};
template <>
struct MoreGeneralNumber<float>: MoreGeneralNumber<long> {};
template <>
struct MoreGeneralNumber<double>: MoreGeneralNumber<float> {};
typedef char (&yes)[1];
typedef char (&no)[2];
template <bool L, bool R, typename LT, typename RT>
struct MoreGeneral { };
template <bool R, typename LT, typename RT>
struct MoreGeneral<true, R, LT, RT> {
typedef LT type;
};
template <typename LT, typename RT>
struct MoreGeneral<false, true, LT, RT> {
typedef RT type;
};
template <typename B, typename D>
struct Host
{
operator B*() const;
operator D*();
};
template <typename B, typename D>
struct is_base_of
{
template <typename T>
static yes check(D*, T);
static no check(B*, int);
static const bool value = sizeof(check(Host<B,D>(), int())) == sizeof(yes);
};
template <typename L, typename R>
struct Deduce: MoreGeneral<is_base_of<MoreGeneralNumber<R>, MoreGeneralNumber<L> >::value,
is_base_of<MoreGeneralNumber<L>, MoreGeneralNumber<R> >::value, L, R > {
};
template <typename ScalarL, typename ScalarR>
struct Multi
{
typename Deduce<ScalarL, ScalarR>::type operator()(ScalarL input1, ScalarR input2) const
{
return input1 * input2;
}
};
int main() {
Multi<int, double> mt;
std::cout << mt(1, 2.5) << std::endl;
}
#包括
模板
结构MoreGeneralNumber{};
模板
结构MoreGeneralNumber:MoreGeneralNumber{};
模板
结构MoreGeneralNumber:MoreGeneralNumber{};
模板
结构MoreGeneralNumber:MoreGeneralNumber{};
typedef字符(&yes)[1];
typedef字符(&no)[2];
模板
结构更一般{};
模板
结构更一般{
typedef-LT型;
};
模板
结构更一般{
typedef-RT型;
};
模板
结构主机
{
运算符B*()常量;
算子D*();
};
模板
结构是的基础
{
模板
静态是检查(D*,T);
静态不检查(B*,int);
静态常量bool value=sizeof(检查(Host(),int())==sizeof(是);
};
模板
结构演绎:更一般{
};
模板
结构多
{
typename推断::类型运算符()(ScalarL input1,ScalarR input2)常量
{
返回input1*input2;
}
};
int main(){
多机器翻译;
标准::cout
如何使用c++03自动获取演绎类型
自动类型推断在C++03中是不可能的。正如在其他答案中提到的,您可能需要手动创建自己的推断专门化
对于C++11/C++14:
// For C++14, simply `auto` is enough
auto operator()(_ScalarL input1, _ScalarR input2) const
#if __cplusplus < 201402L
-> decltype(input1 * input2) // required for C++11
#endif
//对于C++14,只需“auto”就足够了
自动运算符()(\u ScalarL input1,\u ScalarR input2)常量
#如果uu cplusplus<201402L
->decltype(input1*input2)//对于C++11是必需的
#恩迪夫
如果您将演绎类型
重新调用为自动
,它应该已经可以工作了(前提是您使用的是C++14)。请注意,以下划线大写开头的名称是为实现保留的。@KerrekSB对于低g++版本,我不能使用C++11或更高版本………在本文中,“为实现保留”是指“只有编译器供应商才能做到这一点,而您不能”。在c++11之前(因此decltype
)在C++03中,有一个boost::result\u of
,我更愿意编写我自己的东西。@davidhigh的问题是,result\u of将要求原语的运算符*以指针的形式提供函数……不幸的是,我不知道如何使用它reboost::result\u of
可以满足OP需求…啊,好吧,我没有得到,谢谢你的指点。如果是这样,那当然没有什么好处。(对于简单的算术运算,可以使用boost::common\u类型
)。