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

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

于 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 に答える