函数模板的typedef的最佳替代方案? [英] Best alternative to a typedef for a function template?

查看:141
本文介绍了函数模板的typedef的最佳替代方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是这样的:

template <class DataType>
DataType myFunc(DataType in)
{
   ...
}

typedef myFunc<int> myFunc_i;

myFunc_i(37);

...但是,C ++中不能将typedefs用于此类函数.我想知道的是...在这种情况下,人们首选的替代方案是什么?我唯一能想到的是:

...however, typedefs cannot be used for functions like this in C++. What I was wondering is... what are people's preferred alternatives in this case? The only ones I can think of are:

1)处理它,并始终使用myFunc语法 2)手动创建包装函数,即

1) Just deal with it, and always use myFunc syntax 2) Manually create a wrapper function, ie

inline int myFunc_i(int in)
{
    return myFunc<int>(in);
}

这可以工作,但缺点是需要额外的维护,并且可能不同步(即,如果您更改myFunc的函数签名).

This would work, but would have the downside of requiring extra maintenance, and the possibility that it would get out of sync (ie, if you change the function signature for myFunc).

有想法吗?

推荐答案

使用decltype:

template <class DataType>
DataType myFunc(DataType in)
{
   ...
}

using myFunc_i = decltype(myFunc<int>(0));

myFunc_i(37);

这篇关于函数模板的typedef的最佳替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆