如何通过这个简单的例子正确使用 C++11 中的可变参数模板?
How to correctly use variadic templates in C++11 with this simple example?
Using VS2013 (VC2012).
在阅读了许多关于可变参数模板的答案并且我自己的代码失败之后,我想问一下如何编译/实现我的示例,这并不代表我的全部需求,但我更喜欢放置,以便让每个人都更容易、更清晰地理解我的观点。
我最好需要一个接收任意数量的函数(int,const char * 元组),并在函数内访问此列表中的任何元组。由于我相信在互联网上阅读后这是不可能的,所以我尝试使用包含 int 和 const char* 成员的某个类的任意数量来定义可变参数模板,但它失败了。
请注意,我想将声明与不同文件中的定义分开很重要:
短语.h:
class CPhraseParamInfo { // Nothing, for easier example }
class CPhrases
{
template<class... T> void Test(T... paramsInfo);
}
短语.cpp
template<CPhraseParamInfo...> void CPhrases::Test(CPhraseParamInfo... param)
{ // Nothing, for easier example }
错误(翻译):
error C2993: 'CPhraseParamInfo' : invalid type for the template parameter '__formal' without defined type
error C3543: 'CPhraseParamInfo': doesn't contain a parameter pack
error C2244: 'CPhrases::Test' : cannot match the function definition with an existent declaration
请记住,如果可能的话,我更喜欢第一种方法。我希望我足够清楚。
谢谢!
谢谢@Yakk。这是扩展示例,其中包含我实际代码工作的一部分,以展示如何允许将最后一个参数用作传递(对于某些短语va_args处理)的 arbritrary 值,如果有人觉得它有用的话。这里的关键是使用模板调用列表上使用的相同数量的可变参数类调用可变参数模板函数(<CPhraseParamInfo,...>):
短语.h:
class CPhrases:
{
template<class... ParamInfo, typename... Arg> static void
LoadForPlayer(CHL2RP_Player *player, char *dest, int maxlen, const char *header,
const char *prependText, ParamInfo&... paramsInfo, Arg... args)
{
CPhraseParamInfo paramsInfoBuff[] = { paramsInfo... };
LoadForPlayer(player, dest, maxlen, header, prependText, paramsInfoBuff, sizeof paramsInfoBuff, args...);
}
static void LoadForPlayer(CHL2RP_Player *player, char *dest, int maxlen, const char *header, const char *prependText,
CPhraseParamInfo *paramsInfoBuff, int paramCount, ...);
static FORCEINLINE void LoadRegionChat(CHL2RP_Player *player, char *dest, int maxlen, const char *talker, const char *message)
{
LoadForPlayer<CPhraseParamInfo, CPhraseParamInfo>(player, dest, maxlen, REGION_CHAT_HEADER, INDIAN_RED_CHAT_COLOR,
CPhraseParamInfo(CPhraseParamInfo::STRING, TEAM_CHAT_COLOR "%s" DEFAULT_CHAT_COLOR), CPhraseParamInfo(CPhraseParamInfo::STRING, "%s"), talker, message);
}
}
模板函数的定义必须在使用时可见,除非出现异常情况。
你可以这样做:
class CPhraseParamInfo { // Nothing, for easier example }
class CPhrases {
void Test( CPhraseParamInfo* start, CPhraseParamInfo* end );
template<class... T> void Test(T... paramsInfo) {
CPhraseParamInfo buff[]={paramsInfo...};
return Test(buff, buff+sizeof...(paramsInfo));
}
};
然后在您的 cpp 文件中:
void CPhrases::Test(CPhraseParamInfo* start, CPhraseParamInfo* end)
{
// Nothing, for easier example
}
或类似的东西。
相关文章:
- std::具有相同基类的类的变体
- 访问者访问变体并返回不同类型时出错
- 我应该使用什么来代替void作为变体中的替代类型之一
- 将shared_ptr移动到<StructA>shared_ptr<变体<结构A、结构 B>>
- MSVC是否支持C++11样式的属性而不是__declspec
- 创建LinkedList退出,返回代码为-11(SIGSEGV)
- 如何比较自定义类的std::变体
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 继承:构造函数,初始化C++11中基类的类C数组成员
- C 11中的Boost ::变体和多态性
- C++11:变分模板函数参数包扩展执行顺序
- TinyThread计数器 - 便携式c++0和c++11线程间通信变得简单
- C++11 的 decltype 是否使克隆变得不必要?
- C++11:变分齐次非POD模板函数参数
- 将附加参数封装到c++11中的变参数包中
- C++11变分模板
- c++11:用向量的元素调用变差函数
- 为什么c++ 11 CAS操作使用两个指针形参
- c++ 11的构造函数中的花括号形参是什么?
- 理解c++ 11中std::函数的模板实参