11

说我有

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

而且我要

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

要返回"1.10.0"此示例中的等价物,我该怎么做?

我想我需要这两个,在constexpr

  • 整数到字符串的转换
  • 字符串连接

constexpr这个问题纯粹是学术问题,除了“有可能”之外,我认为实际拥有它几乎没有用。我只是看不出这会如何。我愿意接受适用于 GCC 4.9 和 Clang 3.4/3.5 的 C++1y 解决方案。

我相信我在一些日本博客上几乎找到了我想要的东西:

我会看看我能用这些做什么,也许当我对结果感到满意时,我自己会回答这个自称有趣的问题。

4

3 回答 3

15

这是一个小小的 C++1y 解决方案 --- 我想我喜欢 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;
    }
}

现在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;
}

然后我们可以简化用法:

#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)...);
}

还有一个使用示例:

#include <iostream>

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

    std::cout << res.str;
}

直播例子@coliru的clang++3.4

于 2014-05-03T13:43:06.797 回答
8

这是一个 C++11 解决方案。它使用带有char...参数包的类模板来模拟字符串:

#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;
}

活的例子

于 2014-05-04T07:45:00.493 回答
2

这是我快速而肮脏的解决方案:http ://coliru.stacked-crooked.com/a/43c9b365f6435991

它利用了“major.minor.fix”字符串非常短的事实,所以我只使用一个足够大的固定大小的数组来保存字符串。int-to-string 函数用于push_front将字符添加到字符串之前,因此整个字符串从缓冲区的末尾开始增长。

#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';
}

更新:

最好为版本字符串生成创建一个专用类,并将所有功能放入其中:http ://coliru.stacked-crooked.com/a/5e5ee49121cf6205

于 2014-05-03T14:40:23.140 回答