如何通过这个简单的例子正确使用 C++11 中的可变参数模板?

How to correctly use variadic templates in C++11 with this simple example?

本文关键字:变参 C++11 参数 何通过 简单      更新时间:2023-10-16

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
}

或类似的东西。