用于评估模板的C++模板参数(模板模板参数)
C++ Template Parameter that evaluates a Template (template template parameter)
我有一个模板化结构,它使用模板专业化将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;
}
DEMO
此代码警告变量"val"未使用。但是,由于我们不想对getValue()中的类型做什么,所以我就这样留下了代码。
char getValueImpl(char)
{
return 'c';
}
bool getValueImpl(bool)
{
return true;
}
template<int X>
typename IdToType<X>::Type getValue()
{
typename IdToType<X>::Type val;
return getValueImpl(val);
}
int main()
{
std::cout << getValue<2>() << "n";
std::cout << getValue<1>() << "n";
}
相关文章:
- 懒惰的参数评估try_emplace?
- 如何使用 std::forward 精确地评估参数包的扩展?
- 评估参数包
- 在处理之前评估宏参数
- 函数参数计算顺序与 Lambda 捕获评估顺序
- 为什么不能订购函数参数评估
- 评估虚拟堆栈中的可变参数
- C2064:术语未评估为1个参数qtConcurrent :: map的函数
- 在评估不同参数的constexpr函数时,运行时差异
- 在模板参数中评估 constexpr 时 SFINAE 失败
- 完美转发常量参数以进行持续评估
- 未评估上下文中的默认模板参数和 lambda:错误还是功能?
- 构造std ::配对时,评估参数
- std :: initializer_list:错误c2064:项未评估为0参数的函数
- 单步执行函数,但不进入VS2017的参数评估
- 模板参数评估
- 有没有办法只评估对元组的参数
- 评估采用函数参数的元功能,干燥,而无需使用宏
- 函数参数评估顺序
- "IF"参数评估顺序?