用户定义的文字参数不是constexpr? [英] User defined literal arguments are not constexpr?

查看:233
本文介绍了用户定义的文字参数不是constexpr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试用户定义的文字。我想让 _fac 返回数字的阶乘。



让它调用 constexpr 函数工作,但它不让我做模板,因为编译器抱怨的参数不是,不能 constexpr 。 / p>

我很困惑这 - 不是字面量常量表达式?在 5_fac 中的 5 始终是一个可以在编译时评估的文字,所以为什么不能使用

  constexpr int factorial_function(int x){
return(x> 0)? x * factorial_function(x-1):1;
}

constexpr int operator_fac(unsigned long long x){
return factorial_function(x); // this works
}

第二种方法:

  template< int N> struct factorial {
static const unsigned int value = N * factorial< N-1> :: value;
};
模板<> struct factorial< 0> {
static const unsigned int value = 1;
};

constexpr int operator_fac(unsigned long long x){
return factorial_template< x> :: value; //不工作 - x不是constexpr
}


解决方案

这是我最后做的:

  template< typename t> 
constexpr t pow(t base,int exp){
return(exp> 0)? base * pow(base,exp-1):1;
};

template< char ...> struct literal;
模板<> struct literal<> $ {
static const unsigned int to_int = 0;
};
template< char c,char ... cv> struct literal< c,cv ...> {
static const unsigned int to_int =(c - '0')* pow(10,sizeof ...(cv))+ literal< cv ...&
};

template< int N> struct factorial {
static const unsigned int value = N * factorial< N-1> :: value;
};
模板<> struct factorial< 0> {
static const unsigned int value = 1;
};

template< char ... cv>
constexpr unsigned int operator_fac()
{
return factorial< cv ...> :: to_int> :: value;
}

非常感谢KerrekSB!

I'm testing out user defined literals. I want to make _fac return the factorial of the number.

Having it call a constexpr function works, however it doesn't let me do it with templates as the compiler complains that the arguments are not and cannot be constexpr.

I'm confused by this - aren't literals constant expressions? The 5 in 5_fac is always a literal that can be evaluated during compile time, so why can't I use it as such?

First method:

constexpr int factorial_function(int x) {
  return (x > 0) ? x * factorial_function(x - 1) : 1;
}

constexpr int operator "" _fac(unsigned long long x) {
  return factorial_function(x); // this works
}

Second method:

template <int N> struct factorial {
  static const unsigned int value = N * factorial<N - 1>::value;
};
template <> struct factorial<0> {
  static const unsigned int value = 1;
};

constexpr int operator "" _fac(unsigned long long x) {
  return factorial_template<x>::value; // doesn't work - x is not a constexpr
}

解决方案

This is how I ended up doing it:

template <typename t>
constexpr t pow(t base, int exp) {
  return (exp > 0) ? base * pow(base, exp-1) : 1;
};

template <char...> struct literal;
template <> struct literal<> {
  static const unsigned int to_int = 0;
};
template <char c, char ...cv> struct literal<c, cv...> {
  static const unsigned int to_int = (c - '0') * pow(10, sizeof...(cv)) + literal<cv...>::to_int;
};

template <int N> struct factorial {
  static const unsigned int value = N * factorial<N - 1>::value;
};
template <> struct factorial<0> {
  static const unsigned int value = 1;
};

template <char ...cv>
constexpr unsigned int operator "" _fac()
{
  return factorial<literal<cv...>::to_int>::value;
}

Huge thanks to KerrekSB!

这篇关于用户定义的文字参数不是constexpr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆