Say I have
constexpr const std::uint8_t major = 1;
constexpr const std::uint8_t minor = 10;
constexpr const std::uint8_t bugfix = 0;
and I
Here is a C++11 solution. It uses class templates with char... parameter pack to simulate strings:
#include
#include
template
struct String
{
static constexpr char value[] = {symbols...};
};
template
constexpr char String::value[];
template
struct Concat;
template
struct Concat, String>
{
using type = String;
};
template
struct Concatenate;
template
struct Concatenate
{
using type = typename Concat::type>::type;
};
template <>
struct Concatenate<>
{
using type = String<>;
};
template
struct NumberToString
{
using type = typename Concat
<
typename std::conditional<(N >= 10), typename NumberToString::type, String<>>::type,
String<'0' + N % 10>
>::type;
};
template <>
struct NumberToString<0>
{
using type = String<'0'>;
};
constexpr const std::uint8_t major = 1;
constexpr const std::uint8_t minor = 10;
constexpr const std::uint8_t bugfix = 0;
using VersionString = Concatenate
<
NumberToString::type,
String<'.'>,
NumberToString::type,
String<'.'>,
NumberToString::type
>::type;
constexpr const char* version_string = VersionString::value;
int main()
{
std::cout << version_string << std::endl;
}
See live example.