C++ C++;变量模板找出类型
我有以下模板:C++ C++;变量模板找出类型,c++,templates,C++,Templates,我有以下模板: template<size_t DestinationVariableId, size_t SourceVariableId> struct store { static constexpr size_t destinationVariableId = DestinationVariableId; static constexpr size_t sourceVariableId = SourceVariableId; static conste
template<size_t DestinationVariableId, size_t SourceVariableId>
struct store {
static constexpr size_t destinationVariableId = DestinationVariableId;
static constexpr size_t sourceVariableId = SourceVariableId;
static constexpr int instruction = 2;
};
template<size_t DestinationVariableId, size_t SourceVariableId>
struct add {
static constexpr size_t destinationVariableId = DestinationVariableId;
static constexpr size_t sourceVariableId = SourceVariableId;
static constexpr int instruction = 3;
};
/-------------enable_if_else-------------
template<bool Cond, typename TrueType, typename FalseType>
struct enable_if_else;
template<typename TrueType, typename FalseType>
struct enable_if_else<true, TrueType, FalseType> {
using type = TrueType;
};
template<typename TrueType, typename FalseType>
struct enable_if_else<false, TrueType, FalseType> {
using type = FalseType;
};
//-------------static_list-------------
template<class ...Elements>
struct static_list {};
//-------------static_list_get-------------
template<size_t Idx, class List>
struct static_list_get;
template<size_t Idx, class FirstElement, class ...Elements>
struct static_list_get<Idx, static_list<FirstElement, Elements ...>>
: static_list_get<Idx - 1, static_list<Elements ...>> {
};
template<class FirstElement, class ...Elements>
struct static_list_get<1, static_list<FirstElement, Elements ...>> {
using type = FirstElement;
};
//-------------execute_s-------------
template<int index, class ...Commands>
struct execute_s;
template<int index, class ...Commands>
struct execute_s <index, static_list<Commands ...>> {
static constexpr int value = enable_if_else < static_list_get<index, static_list<Commands ...>>::instruction == 1, add<0, 0>::instruction , store<1, 1>::instruction >;
};
模板
结构存储{
静态constexpr size\u t destinationVariableId=destinationVariableId;
静态constexpr size\u t sourceVariableId=sourceVariableId;
静态constexpr int指令=2;
};
模板
结构添加{
静态constexpr size\u t destinationVariableId=destinationVariableId;
静态constexpr size\u t sourceVariableId=sourceVariableId;
静态constexpr int指令=3;
};
/-------------如果需要,请启用-------------
模板
如果需要,则结构启用;
模板
结构启用\u如果\u其他{
使用类型=TrueType;
};
模板
结构启用\u如果\u其他{
使用类型=假类型;
};
//-------------静态列表-------------
模板
结构静态_列表{};
//-------------静态列表-------------
模板
结构静态列表获取;
模板
结构静态列表获取
:静态\u列表\u获取{
};
模板
结构静态列表获取{
使用type=FirstElement;
};
//-------------执行-------------
模板
结构执行;
模板
结构执行{
static constexpr int value=enable_if_else;
};
在execute_中,我想检查给定索引处的命令是否为add或store类型,并返回给定的输出
如何在编译时在模板结构中检查给定索引处的命令是什么类型的?
enable_if_else代码>不起作用,因为::指令在常规命令中不清晰。::指令是一个int
,因此您不能将其作为参数传递给enable\u if\u else
,至少作为一种类型。您可以将其作为非类型模板参数传递,但实际上,您根本不需要enable\u if\u else
。您可以像这样初始化值
:
static constexpr int value = [] {
return static_list_get<index, static_list<Commands ...>>::instruction == 1 ?
add<0, 0>::instruction :
store<1, 1>::instruction;
}();
static constexpr int value=[]{
返回静态\u列表\u get::指令==1?
添加::说明:
存储:指令;
}();
请将编译器错误消息添加到问题中。另外,请演示如何使用execute\s
类。请参阅如何生成一个错误,因为它给出了一个指令不是…的成员的错误。。。。