How to generate a constexpr version string from three integers (or perhaps a git/SVN commit/rev. string)?
Asked Answered
C

3

11

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 want

constexpr const char* version_string(){ ... }

to return the equivalent of "1.10.0" in this example, how would I do it?

I assume I'll need both of these, in constexpr:

  • integer to string conversion
  • string concatenation

The problem is purely academic, and I see little to no use to actually have it constexpr other than "it's possible". I just can't see how this would pan out. I'm willing to accept C++1y solutions that work on GCC 4.9 and Clang 3.4/3.5.

I believe I have found nearly what I seek on some Japanese blogs:

I will see what I can do with these, and perhaps answer this self-declared interesting question myself when I'm satisfied with the result.

Cockcrow answered 3/5, 2014 at 13:5 Comment(2)
I can see a solution combining the results you linked (using strcat(itoa(major),strcat(".",strcat(itoa(minor),strcat(".",itoa(bugfix))))) or some simplified variant...Commissariat
if the version constants are generated by a tool it could also generate teh stringSpinelli
S
9

Here is a C++11 solution. It uses class templates with char... parameter pack to simulate strings:

#include <iostream>
#include <type_traits>

template <char... symbols>
struct String
{
    static constexpr char value[] = {symbols...};
};

template <char... symbols>
constexpr char String<symbols...>::value[];

template <typename, typename>
struct Concat;

template <char... symbols1, char... symbols2>
struct Concat<String<symbols1...>, String<symbols2...>>
{
    using type = String<symbols1..., symbols2...>;
};

template <typename...>
struct Concatenate;

template <typename S, typename... Strings>
struct Concatenate<S, Strings...>
{
    using type = typename Concat<S, typename Concatenate<Strings...>::type>::type;
};

template <>
struct Concatenate<>
{
    using type = String<>;
};

template <std::size_t N>
struct NumberToString
{
    using type = typename Concat
        <
            typename std::conditional<(N >= 10), typename NumberToString<N / 10>::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<major>::type,
        String<'.'>,
        NumberToString<minor>::type,
        String<'.'>,
        NumberToString<bugfix>::type
    >::type;

constexpr const char* version_string = VersionString::value;

int main()
{
    std::cout << version_string << std::endl;
}

See live example.

Starstudded answered 4/5, 2014 at 7:45 Comment(1)
This looks quite elegant and straightforward, and pretty great really :-)Cockcrow
E
15

Here's a little C++1y solution --- I think I LOVE C++1y.

#include <utility>

template<int N>
struct c_string
{
    int length;
    char str[N+1];

    constexpr explicit c_string(int p_length)
        : length(p_length), str{}
    {}
};

template<int M>
constexpr auto make_c_string(char const (&str)[M])
{
    c_string<M-1> ret{M-1};
    for(int i = 0; i < M; ++i)
    {
        ret.str[i] = str[i];
    }
    return ret;
}

template<int N, int M>
constexpr auto join(c_string<N> const& x, c_string<M> const& y)
{
    c_string<N+M> ret{x.length + y.length};

    for(int i = 0; i < x.length; ++i)
    {
        ret.str[i] = x.str[i];
    }
    for(int i = 0; i < y.length; ++i)
    {
        ret.str[i+x.length] = y.str[i];
    }

    ret.str[N+M] = '\0';

    return ret;
}

template<int N, int M>
constexpr auto operator+(c_string<N> const& x, c_string<M> const& y)
{
    return join(x, y);
}


template<class T>
constexpr void c_swap(T& x, T& y)
{
    T tmp( std::move(x) );
    x = std::move(y);
    y = std::move(tmp);
}

// from http://en.cppreference.com/w/cpp/algorithm/reverse
template<class I>
constexpr void reverse(I beg, I end)
{
    while(beg != end && beg != --end)
    {
        c_swap(*beg, *end);
        ++beg;
    }
}

Now the constexpr itoa:

#include <limits>

template<class T>
constexpr auto c_abs(T x)
{
    return x < T{0} ? -x : x;
}

template<class T>
constexpr auto ntoa(T n)
{
    c_string< std::numeric_limits<T>::digits10 + 1 > ret{0};
    int pos = 0;

    T cn = n;
    do
    {
        ret.str[pos] = '0' + c_abs(cn % 10);
        ++pos;
        cn /= 10;
    }while(cn != T{0});

    if(n < T{0})
    {
        ret.str[pos] = '-';
        ++pos;
    }

    ret.str[pos] = '\0';
    ret.length = pos;

    reverse(ret.str, ret.str+ret.length);
    return ret;
}

We can then simplify the usage:

#include <type_traits>

// not supported by the libstdc++ at coliru
//template<class T, class = std::enable_if_t< std::is_arithmetic<T>{} >>
template<class T, class = typename std::enable_if<std::is_arithmetic<T>{}>::type>
constexpr auto to_c_string(T p)
{
    return ntoa(p);
}
template<int N>
constexpr auto to_c_string(char const (&str)[N])
{
    return make_c_string(str);
}

template<class T, class U, class... TT>
constexpr auto to_c_string(T&& p0, U&& p1, TT&&... params)
{
    return   to_c_string(std::forward<T>(p0))
           + to_c_string(std::forward<U>(p1), std::forward<TT>(params)...);
}

And a usage example:

#include <iostream>

int main()
{
    constexpr auto res = to_c_string(42," is the solution, or is it ",-21,"?");

    std::cout << res.str;
}

Live example @ coliru's clang++3.4

Entellus answered 3/5, 2014 at 13:43 Comment(2)
Something new learned. Anyway, why an int index and not size_t or ssize_t?Atrocity
@Atrocity First, it is slightly shorter; and second, it has the words "DON'T PANIC" inscribed .. no, wait.. see channel9.msdn.com/Events/GoingNative/2013/… 12:12-13:08 and especially 42:42-45:30Entellus
S
9

Here is a C++11 solution. It uses class templates with char... parameter pack to simulate strings:

#include <iostream>
#include <type_traits>

template <char... symbols>
struct String
{
    static constexpr char value[] = {symbols...};
};

template <char... symbols>
constexpr char String<symbols...>::value[];

template <typename, typename>
struct Concat;

template <char... symbols1, char... symbols2>
struct Concat<String<symbols1...>, String<symbols2...>>
{
    using type = String<symbols1..., symbols2...>;
};

template <typename...>
struct Concatenate;

template <typename S, typename... Strings>
struct Concatenate<S, Strings...>
{
    using type = typename Concat<S, typename Concatenate<Strings...>::type>::type;
};

template <>
struct Concatenate<>
{
    using type = String<>;
};

template <std::size_t N>
struct NumberToString
{
    using type = typename Concat
        <
            typename std::conditional<(N >= 10), typename NumberToString<N / 10>::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<major>::type,
        String<'.'>,
        NumberToString<minor>::type,
        String<'.'>,
        NumberToString<bugfix>::type
    >::type;

constexpr const char* version_string = VersionString::value;

int main()
{
    std::cout << version_string << std::endl;
}

See live example.

Starstudded answered 4/5, 2014 at 7:45 Comment(1)
This looks quite elegant and straightforward, and pretty great really :-)Cockcrow
D
2

Here is my quick and dirty solution: http://coliru.stacked-crooked.com/a/43c9b365f6435991

It exploits the fact that a 'major.minor.fix' string will be quite short, so I just use a fixed-size array big enough to hold the string. The int-to-string function uses push_front to prepend characters to string, so the whole string grows from end of buffer.

#include <cstdint>

constexpr const std::uint8_t major = 1;
constexpr const std::uint8_t minor = 10;
constexpr const std::uint8_t bugfix = 0;

struct char_array {
    constexpr char_array() : ofs{sizeof(value) - 1}, value{} {}
    constexpr const char* c_str() const { return value + ofs; }

    constexpr void push_front(char c) {
        --ofs;
        value[ofs] = c;
    }

private:
    int ofs;
    char value[42]; // big enough to hold version string
};

constexpr char_array predend_int(char_array a, int x) {
    do {
        auto digit = x % 10;
        x = x / 10;
        a.push_front(digit + '0');
    }
    while (x);
    return a;   
}

constexpr auto version_string() {
    char_array a;
    a = predend_int(a, bugfix);
    a.push_front('.');
    a = predend_int(a, minor);
    a.push_front('.');
    a = predend_int(a, major);
    return a;
}

#include <iostream>
int main() {
    constexpr char_array ver = version_string();
    std::cout << ver.c_str() << '\n';
}

Update:

It's probably better to make a dedicated class for version string generation, and put all the functions in it: http://coliru.stacked-crooked.com/a/5e5ee49121cf6205

Disaffect answered 3/5, 2014 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.