C++ C++;评估模板的模板参数(模板参数)
我有一个模板结构,它使用模板专门化(取自)将ID映射到类型C++ C++;评估模板的模板参数(模板参数),c++,template-meta-programming,C++,Template Meta Programming,我有一个模板结构,它使用模板专门化(取自)将ID映射到类型 模板 结构IdToType {}; 样板 结构IdToType { 类型定义布尔类型; }; 样板 结构IdToType { typedef字符类型; }; 现在我想调用这样的函数 getValue() 其中,函数的返回值是ID的对应类型 template</*.... I don't know what to put here...*/ T> idToType<T>::Type getValue() // I
模板
结构IdToType
{};
样板
结构IdToType
{
类型定义布尔类型;
};
样板
结构IdToType
{
typedef字符类型;
};
现在我想调用这样的函数
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
}
templatetemplate
typename IdToType::Type getValue()
{
使用T=typename IdToType::Type;
返回65;
}
模板
typename IdToType::Type getValue()
{
使用T=typename IdToType::Type;
返回65;
}
此代码警告未使用变量“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";
}
char getValueImpl(char)
{
返回“c”;
}
bool getValueImpl(bool)
{
返回true;
}
样板
typename IdToType::Type getValue()
{
typename-IdToType::Type-val;
返回getValueImpl(val);
}
int main()
{
std::cout此代码警告变量“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";
}
char getValueImpl(char)
{
返回“c”;
}
bool getValueImpl(bool)
{
返回true;
}
样板
typename IdToType::Type getValue()
{
typename-IdToType::Type-val;
返回getValueImpl(val);
}
int main()
{
标准::cout
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";
}