我有一个模板化的结构,使用模板专用化将ID映射到一个类型(取自https://www.justsoftwaresolutions.co.uk/articles/exprtype.pdf)。
template<int id>
struct IdToType
{};
template<>
struct IdToType<1>
{
typedef bool Type;
};
template<>
struct IdToType<2>
{
typedef char Type;
};
现在我想调用像这样的函数getValue()
其中函数的返回值是ID的相应类型。
template</*.... I don't know what to put here...*/ T>
idToType<T>::Type getValue() // I don't know exactly how to define the return value
{
// whant to do some things with the provided ID and with the type of the id
}
简而言之:-我想要一个模板化函数,可以在其中使用ID作为模板参数。-函数需要具有与ID对应的类型作为返回值(我从IdToType :: Type获得对应的类型)。-在函数的主体中,我想访问ID和ID的类型。-我认为使用模板模板参数应该可行。但是我不确定。
我希望这很清楚...
提前致谢!
template <int id>
typename IdToType<id>::Type getValue()
{
using T = typename IdToType<id>::Type;
return 65;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句