5
\$\begingroup\$

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
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

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
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.