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

1 Answer 1

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.