如何从三个整数(或者一个git / SVN commit / rev。string)生成一个constexpr版本字符串? [英] How to generate a constexpr version string from three integers (or perhaps a git/SVN commit/rev. string)?
问题描述
说我有
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解决方案。
我相信我已经找到了一些日本博客:
我会看到我能做什么
这里有一个小小的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 {
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 {
ret.str [i] = x.str [i]
}
for(int i = 0; i {
ret.str [i + x.length] = y.str [一世];
}
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);
}
模板< class T>
constexpr void c_swap(T& x,T& y)
{
T tmp(std :: move(x));
x = std :: move(y);
y = std :: move(tmp);
}
//来自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>
//在coliru的libstdc ++不支持
// 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& ))
+ to_c_string(std :: forward< U>(p1),std :: forward< TT(params)...)
}
并且使用示例:
#include< iostream>
int main()
{
constexpr auto res = to_c_string(42,是解决方案,或者是,-21,?
std :: cout<< res.str;
}
Live example @ coliru's clang ++ 3.4
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.
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
这篇关于如何从三个整数(或者一个git / SVN commit / rev。string)生成一个constexpr版本字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!