Вычисление не типовых параметров шаблона - PullRequest
0 голосов
/ 23 мая 2018

Можно ли вывести значение шаблона (не типа) для функции c ++ 17?

Функция foo:

template<int I>
int foo()
{
    return (I);
}

Может быть вызвана через:

foo<5>();

И вернет 5.

Типы шаблонов могут быть выведены через тип аргумента функции.Можно ли как-то сделать то же самое для значения шаблона?Например:

template<int I = x>
int bar(const int x)
{
    return (I);
}

Это, очевидно, не сработает (потому что для одного x требуется до его объявления), но может быть какой-то трюк C ++ 17, который бы позволил это?

Я хотел бы использовать это как способ установки аргумента функции константного выражения.

Ответы [ 2 ]

0 голосов
/ 23 мая 2018
template<auto x>
using constant = std::integral_constant< std::decay_t<decltype(x)>, x >;

шаблон

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

или что-то подобное.Теперь:

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

shoukd работает с bar(5_k);.У меня могут быть некоторые опечатки, и причудливый шаблон auto constant может заблокировать вычет, а поддержка 0X и 0B отсутствует.Но кроме этого - звук.


Альтернативная реализация на основе цикла:

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

живые примеры .

0 голосов
/ 23 мая 2018

То, что вы хотите, может быть сделано только с помощью (ab), используя тип удержания для целочисленного вывода.Заметьте:

template<int x>
struct integer_value {};

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

Конечно, вы должны вызывать это с test(integer_value<4>{}) или чем-то подобным.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...