Template non-type parameter deduction

怎甘沉沦 提交于 2019-12-05 11:42:32

What you want can only be done by (ab)using type deduction for integer deduction. Observe:

template<int x>
struct integer_value {};

template<int x>
void test(integer_value<x> val)
{
  //x can be used here.
}

Of course, you must invoke this with test(integer_value<4>{}) or something similar.

template<auto x>
using constant = std::integral_constant< std::decay_t<decltype(x)>, x >;

template

using constant = std::integral_constant< std::decay_t<decltype(x)>, x >;
constexpr int digit_val( char c ) {
    if (c >= '0' && c <='9')
        return c-'0';
    else if (c >= 'A' && c <= 'Z')
        return c-'A'+10;
    else if (c >= 'a' && c <= 'z')
        return c-'a'+10;
    else
        throw nullptr;
}
constexpr long long ce_pow( int base, int count, long long acc=1 ){
  if (!count) return acc;
  return ce_pow( base, count-1, acc*(long long)base );
}
constexpr long long from_digits( long long acc, int base ){
  return acc;
}
template<int I, int...Is>
constexpr long long from_digits( long long acc, int base, constant<I>, constant<Is>... ) {
  return from_digits( acc+ce_pow(base, sizeof...(Is))*(long long)I, base, constant<Is>{}... );
}
template<char...cs>
constexpr long long val( constant<'0'>, constant<'x'>, constant<cs>... ){
  return from_digits( 0, 16, constant<digit_val(cs)>{}... );
}
template<char...cs>
constexpr long long val( constant<'0'>, constant<'b'>, constant<cs>... ){
  return from_digits( 0, 2, constant<digit_val(cs)>{}... );
}
template<char...cs>
constexpr long long val( constant<'0'>, constant<cs>... ){
  return from_digits( 0, 8, constant<digit_val(cs)>{}... );
}
template<char...cs>
constexpr auto operator""_k(){
    return constant< val( constant<cs>{}... ) >{};
}

or somesuch. Now:

template<int I>
int bar(constant<I>)
{
  return (I);
}

shoukd work with bar(5_k);. I may have some typos, and the fancy auto constant template may block deduction, and 0X and 0B support is missing. But other than that is sound.


Alternative loop based implementation:

struct number_format {
    int prefix = 0;
    int base = 0;
};
template<char...cs>
constexpr number_format get_format( constant<'0'>, constant<'x'>, constant<cs>... ) {
    return {2,16};
}
template<char...cs>
constexpr number_format get_format( constant<'0'>, constant<'X'>, constant<cs>... ) {
    return {2,16};
}
template<char...cs>
constexpr number_format get_format( constant<'0'>, constant<'b'>, constant<cs>... ) {
    return {2,2};
}
template<char...cs>
constexpr number_format get_format( constant<'0'>, constant<'B'>, constant<cs>... ) {
    return {2,2};
}
template<char...cs>
constexpr number_format get_format( constant<'0'>, constant<cs>... ) {
    return {1,8};
}
template<char...cs>
constexpr number_format get_format( constant<cs>... ) {
    return {0,10};
}
template<char...Cs>
constexpr long long val( constant<Cs>...cs ){
  char buff[] = {Cs...};
  constexpr number_format fmt = get_format( cs... );

  long long r = 0;
  for (auto it = std::begin(buff)+fmt.prefix; it != std::end(buff); ++it) {
      r *= fmt.base;
      r += digit_val(*it);
  }
  return r;
}
template<char...cs>
constexpr auto operator""_k(){
    return constant< val( constant<cs>{}... ) >{};
}

live examples.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!