\$\begingroup\$
\$\endgroup\$
These are some utility functions that I use for building error messages:
SQLUtil.h
#ifndef THORS_ANVIL_SQL_SQL_UTIL_H
#define THORS_ANVIL_SQL_SQL_UTIL_H
#include <map>
#include <string>
#include <sstream>
namespace ThorsAnvil
{
namespace SQL
{
using Options=std::map<std::string, std::string>;
}
/* The following is generic to all ThorsAnvil Code */
/* Build error message or bug report for exceptions */
template<typename T>
void printItem(std::ostream& str, T const& msg)
{
str << msg;
}
template<typename... Args>
std::string stringBuild(Args const& ...a)
{
std::stringstream msg;
auto list = {0, (printItem(msg, a), 0) ...};
(void)list;
return msg.str();
}
template<typename... Args>
std::string errorMsg(Args const& ...a)
{
return stringBuild(a...);
}
template<typename... Args>
std::string bugReport(Args const& ...a)
{
return stringBuild(a..., "\nPlease File a Bug Report: ");
}
}
#endif
G. Sliepen
68.7k3 gold badges74 silver badges179 bronze badges
asked Mar 17, 2017 at 15:35
1 Answer 1
\$\begingroup\$
\$\endgroup\$
The ThorsAnvil::SQL::Options
type is defined, but never used, so it's hard to say much about it.
That's a clever way to get around the lack of fold-expressions in C++14:
auto list = {0, (printItem(msg, a), 0) ...}; (void)list;
I would probably use a version conditional, to show the intent before the workaround:
#if __cplusplus >= 201703L
(msg << ... << a);
#else
auto list = {0, (printItem(msg, a), 0) ...};
(void)list;
#endif
answered Jan 23, 2022 at 14:04
lang-cpp