可变参数模板:如何在参数中 "look ahead"
Variadic Templates: how to "look ahead" in the arguments
我正在实施一个 printf
版本,它也可以处理 std::string
个参数。
它的核心是这些函数:
// use sprintf to transform the single format string with the value t
template<typename T>
std::string simpleFormat(const std::string sFormat, const T t) {
size_t required = snprintf(NULL, 0, sFormat.c_str(), t);
char sTemp[required+1];
sprintf(sTemp, sFormat.c_str(), t);
return std::string(sTemp);
}
// termination of recursion
std::string recursiveFormat(stringvec &vParts, stringvec &vFormats, uint i) {
return vParts[i];
}
// recursively walk through arguments
template<typename T, typename... Args>
std::string recursiveFormat(stringvec &vParts, stringvec &vFormats, uint i, T value, Args... args) {
std::string sRes = "";
if (i < vFormats.size()) {
sRes += vParts[i];
sRes += simpleFormat(vFormats[i], value);
sRes += recursiveFormat(vParts, vFormats, i+1, args...);
}
return sRes;
}
这个效果很好。但是,要处理带有星号的格式字符串,例如 printf("%0*d", width, value)
这种方法不起作用。
我尝试添加两个函数
// get the next argument and return it
template<typename U, typename... Args>
U fetchNextParam(const U value2, Args... args) {
return value2;
}
// handle the star by providing sprintf with two values
template<typename T, typename U>
std::string starFormat(const std::string sFormat, const T value1, const U value2) {
size_t required = snprintf(NULL, 0, sFormat.c_str(), value1, value2);
char sTemp[required+1];
sprintf(sTemp, sFormat.c_str(), value1, value2);
return std::string(sTemp);
}
并像这样修改 recursiveFormat()
函数:
template<typename T, typename... Args>
std::string recursiveFormat(stringvec &vParts, stringvec &vFormats, uint i, T value, Args... args) {
std::string sRes = "";
if (i < vFormats.size()) {
sRes += vParts[i];
//--- trying to handle star
if (vFormats[i].find('*') != std::string::npos) {
auto value2 = fetchNextParam(args...);
sRes += starFormat(vFormats[i], value, value2);
} else {
sRes += simpleFormat(vFormats[i], value);
}
sRes += recursiveFormat(vParts, vFormats, i+1, args...);
}
return sRes;
}
但是,此代码无法编译:我收到编译器消息:
sptest.cpp: In instantiation of ‘std::string recursiveFormat(stringvec&, stringvec&, uint, T, Args ...) [with T = double; Args = {}; std::string = std::__cxx11::basic_string<char>; stringvec = std::vector<std::__cxx11::basic_string<char> >; uint = unsigned int]’:
sptest.cpp:114:32: recursively required from ‘std::string recursiveFormat(stringvec&, stringvec&, uint, T, Args ...) [with T = int; Args = {double}; std::string = std::__cxx11::basic_string<char>; stringvec = std::vector<std::__cxx11::basic_string<char> >; uint = unsigned int]’
sptest.cpp:114:32: required from ‘std::string recursiveFormat(stringvec&, stringvec&, uint, T, Args ...) [with T = int; Args = {int, double}; std::string = std::__cxx11::basic_string<char>; stringvec = std::vector<std::__cxx11::basic_string<char> >; uint = unsigned int]’
sptest.cpp:124:36: required from here
sptest.cpp:107:41: error: no matching function for call to ‘fetchNextParam()’
107 | auto value2 = fetchNextParam(args...);
| ~~~~~~~~~~~~~~^~~~~~~~~
sptest.cpp:58:3: note: candidate: ‘template<class U, class ... Args> U fetchNextParam(U, Args ...)’
58 | U fetchNextParam(const U value2, Args... args) {
| ^~~~~~~~~~~~~~
sptest.cpp:58:3: note: template argument deduction/substitution failed:
sptest.cpp:107:41: note: candidate expects at least 1 argument, 0 provided
107 | auto value2 = fetchNextParam(args...);
| ~~~~~~~~~~~~~~^~~~~~~~~
我必须做什么才能使可变参数中的功能看起来“领先”?
您可以使用 if constexpr
(C++17) 仅当有足够的参数时激活代码:
if (vFormats[i].find('*') != std::string::npos) {
if constexpr (sizeof...(Args) > 0) {
auto value2 = fetchNextParam(args...); // std::get<0>(std::tie(args...))
sRes += starFormat(vFormats[i], value, value2);
} else {
throw std::runtime_error("Wrong number of argument");
}
} else {
sRes += simpleFormat(vFormats[i], value);
}
您还可以提供fetchNextParam(/*empty parameter*/)
// it's type need to return something since you store the value into value2
int fetchNextParam(){throw std::runtime_error("Wrong number of argument");}
我正在实施一个 printf
版本,它也可以处理 std::string
个参数。
它的核心是这些函数:
// use sprintf to transform the single format string with the value t
template<typename T>
std::string simpleFormat(const std::string sFormat, const T t) {
size_t required = snprintf(NULL, 0, sFormat.c_str(), t);
char sTemp[required+1];
sprintf(sTemp, sFormat.c_str(), t);
return std::string(sTemp);
}
// termination of recursion
std::string recursiveFormat(stringvec &vParts, stringvec &vFormats, uint i) {
return vParts[i];
}
// recursively walk through arguments
template<typename T, typename... Args>
std::string recursiveFormat(stringvec &vParts, stringvec &vFormats, uint i, T value, Args... args) {
std::string sRes = "";
if (i < vFormats.size()) {
sRes += vParts[i];
sRes += simpleFormat(vFormats[i], value);
sRes += recursiveFormat(vParts, vFormats, i+1, args...);
}
return sRes;
}
这个效果很好。但是,要处理带有星号的格式字符串,例如 printf("%0*d", width, value)
这种方法不起作用。
我尝试添加两个函数
// get the next argument and return it
template<typename U, typename... Args>
U fetchNextParam(const U value2, Args... args) {
return value2;
}
// handle the star by providing sprintf with two values
template<typename T, typename U>
std::string starFormat(const std::string sFormat, const T value1, const U value2) {
size_t required = snprintf(NULL, 0, sFormat.c_str(), value1, value2);
char sTemp[required+1];
sprintf(sTemp, sFormat.c_str(), value1, value2);
return std::string(sTemp);
}
并像这样修改 recursiveFormat()
函数:
template<typename T, typename... Args>
std::string recursiveFormat(stringvec &vParts, stringvec &vFormats, uint i, T value, Args... args) {
std::string sRes = "";
if (i < vFormats.size()) {
sRes += vParts[i];
//--- trying to handle star
if (vFormats[i].find('*') != std::string::npos) {
auto value2 = fetchNextParam(args...);
sRes += starFormat(vFormats[i], value, value2);
} else {
sRes += simpleFormat(vFormats[i], value);
}
sRes += recursiveFormat(vParts, vFormats, i+1, args...);
}
return sRes;
}
但是,此代码无法编译:我收到编译器消息:
sptest.cpp: In instantiation of ‘std::string recursiveFormat(stringvec&, stringvec&, uint, T, Args ...) [with T = double; Args = {}; std::string = std::__cxx11::basic_string<char>; stringvec = std::vector<std::__cxx11::basic_string<char> >; uint = unsigned int]’:
sptest.cpp:114:32: recursively required from ‘std::string recursiveFormat(stringvec&, stringvec&, uint, T, Args ...) [with T = int; Args = {double}; std::string = std::__cxx11::basic_string<char>; stringvec = std::vector<std::__cxx11::basic_string<char> >; uint = unsigned int]’
sptest.cpp:114:32: required from ‘std::string recursiveFormat(stringvec&, stringvec&, uint, T, Args ...) [with T = int; Args = {int, double}; std::string = std::__cxx11::basic_string<char>; stringvec = std::vector<std::__cxx11::basic_string<char> >; uint = unsigned int]’
sptest.cpp:124:36: required from here
sptest.cpp:107:41: error: no matching function for call to ‘fetchNextParam()’
107 | auto value2 = fetchNextParam(args...);
| ~~~~~~~~~~~~~~^~~~~~~~~
sptest.cpp:58:3: note: candidate: ‘template<class U, class ... Args> U fetchNextParam(U, Args ...)’
58 | U fetchNextParam(const U value2, Args... args) {
| ^~~~~~~~~~~~~~
sptest.cpp:58:3: note: template argument deduction/substitution failed:
sptest.cpp:107:41: note: candidate expects at least 1 argument, 0 provided
107 | auto value2 = fetchNextParam(args...);
| ~~~~~~~~~~~~~~^~~~~~~~~
我必须做什么才能使可变参数中的功能看起来“领先”?
您可以使用 if constexpr
(C++17) 仅当有足够的参数时激活代码:
if (vFormats[i].find('*') != std::string::npos) {
if constexpr (sizeof...(Args) > 0) {
auto value2 = fetchNextParam(args...); // std::get<0>(std::tie(args...))
sRes += starFormat(vFormats[i], value, value2);
} else {
throw std::runtime_error("Wrong number of argument");
}
} else {
sRes += simpleFormat(vFormats[i], value);
}
您还可以提供fetchNextParam(/*empty parameter*/)
// it's type need to return something since you store the value into value2
int fetchNextParam(){throw std::runtime_error("Wrong number of argument");}