C++ va_list函数重载
c++ va_list function overload
我目前有 2 个函数重载:
void log(const char* format, ...);
void log(const string& message);
我希望在此调用的情况下:log("hello");
调用字符串版本,或者换句话说,只有在 2 个或更多参数的情况下才应调用第一个重载。
我想过这样做:
template<typename T>
void log(const char* format, T first, ...);
但在这种情况下,我将无法在代码中正确使用va_list
。
我可能缺少其他解决方案吗?
编辑:我想过从函数内部检查va_list
的大小,并在 0 的情况下重定向,但据我了解,不可能获得 va_list
的大小。
- 力型结构:
log(std::string{"hello})
。但这似乎并不是你想要的。 -
在任一函数中,调用另一个函数。
void log(const string& s) { log(s.c_str()); }
但它不是很有效,因为您将拥有一个无用的
string
对象,尽管编译器可能能够内联调用。 -
使用可变参数模板和 SFINAE:
void log(const string&); auto log(const char *ptr, Args&& ... args) -> typename std::enable_if<sizeof...(Args) != 0, void>::type
仅当存在尾随参数时,第二个重载才会在候选函数集中可用。展示。在 C++14 中,您可以使用
std::enable_if
的速记版本,std::enable_if_t
,这使得语法更加清晰:auto log(const char *ptr, Args&& ... args) -> std::enable_if_t<sizeof...(Args) != 0, void>
您仍然可以在 C++11 中使用
template <bool B, typename T> using enable_if_t = typename std::enable_if<B, T>::type;
如果你调用一个接受va_list
的函数(比如printf
),你仍然可以扩展参数包:
std::printf(ptr, args...);
但反之亦然。
您可以省略"printf"函数样式并使用流操纵器(采用可变数量的参数(可变参数模板)):
// Header
// ============================================================================
#include <iostream>
#include <sstream>
#include <tuple>
// Format
// ============================================================================
namespace Detail {
template<unsigned I, unsigned N>
struct Format;
template<unsigned N>
struct Format<N, N> {
template<typename... Args>
static void write(std::ostream&, const std::tuple<Args...>&, std::size_t offset)
{}
};
template<unsigned I, unsigned N>
struct Format {
template<typename... Args>
static void write(
std::ostream& stream,
const std::tuple<Args...>& values,
std::size_t offset)
{
if(offset == 0) stream << std::get<I>(values);
else Format<I+1, N>::write(stream, values, offset - 1);
}
};
class FormatParser
{
public:
const char* fmt;
const std::size_t size;
FormatParser(const char* fmt, std::size_t size)
: fmt(fmt), size(size)
{}
virtual ~FormatParser() {}
void write(std::ostream& stream) const;
protected:
virtual void write_value(std::ostream&, std::size_t index) const = 0;
};
} // namespace Detail
template<typename... Args>
class Format : public Detail::FormatParser
{
public:
typedef std::tuple<const Args&...> Tuple;
static constexpr std::size_t Size = std::tuple_size<Tuple>::value;
const std::tuple<const Args&...> values;
Format(const char* fmt, const Args&... values)
: Detail::FormatParser(fmt, Size), values(values...)
{}
protected:
void write_value(std::ostream& stream, std::size_t index) const {
Detail::Format<0, Size>::write(stream, values, index);
}
};
template <typename... Args>
inline Format<Args...> format(const char* fmt, const Args&... values) {
return Format<Args...>(fmt, values...);
}
template <typename... Args>
inline std::ostream& operator << (std::ostream& stream, const Format<Args...>& format) {
format.write(stream);
return stream;
}
template <typename... Args>
inline std::string format_string(const char* fmt, const Args&... values) {
std::ostringstream result;
result << format(fmt, values...);
return result.str();
}
// Source
// ============================================================================
#include <cctype>
#include <cstdlib>
#include <stdexcept>
namespace Detail {
void FormatParser::write(std::ostream& stream) const {
const char* s = fmt;
while(*s) {
switch(*s) {
case '{':
if(*(++s) != '{') {
char* end;
unsigned long index = std::strtoul(s, &end, 10);
while(*end != '}') {
if( ! std::isspace(*end)) {
s = end;
if( ! *s) s = "End";
throw std::runtime_error(std::string(
"Invalid Format String `") + fmt + "` at " + s);
}
++end;
}
s = end + 1;
if(index < size) write_value(stream, index);
else throw std::runtime_error(std::string(
"Invalid Format Index `") + std::to_string(index)
+ "` in `" + fmt + '`');
continue;
}
break;
case '}':
if(*(++s) != '}') {
if( ! *s) s = "End";
throw std::runtime_error(
std::string("Invalid Format String `") + fmt + "`"
"Missing `}` at " + s);
}
break;
}
stream.put(*s++);
}
}
} // namespace Detail
// Usage
// ============================================================================
int main() {
// a = 1; b = 2; 1 + 2 = 3
std::cout << format("a = {0}; b = {1}; {0} + {1} = {2}", 1, 2, 3) << "n";
}
另外:看看 boost::format
相关文章:
- 为什么使用SFINAE而不是函数重载
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- c++:可变模板和函数重载
- 在缺少函数重载时抛出异常,并带有 std::variant 而不是编译时错误
- 解决模板成员函数重载
- 为什么不允许成员函数和非成员函数之间的函数重载?
- 推断模板化函数中的函数重载
- C++复制函数重载导致"must be a nonstatic member function"错误
- 为什么 std::sort 找不到合适的(静态成员)函数重载?
- 可变参数泛型 lambda 和函数重载
- C++中的函数重载和继承
- 当有右值构造函数可用时,为什么从右值调用类引用构造函数重载?
- C/C++ 可变参数宏函数重载
- 将基类的成员函数重载到其他派生类C++
- C++ 函数重载匹配
- C++函数重载,具体步骤是什么
- C++:使用 param pack 显式调用函数重载
- 隐式生成的函数重载用于右值参数?
- 使用函数重载输入运算符
- 运算符重载函数上的函数重载