小编Kam*_*rek的帖子

可能的模板和constexpr - 如果不兼容

我想e在编译时计算价值(不用担心,不是作业),但出了点问题.

template<size_t limit = 3, class result = std::ratio<0, 1>, size_t factorial = 1, size_t count = 1>
constexpr double e_impl() {
    if constexpr(limit == 0) {
        return static_cast<double>(result{}.num) / result{}.den;
    }
    return e_impl<limit - 1, std::ratio_add<result, std::ratio<1, factorial>>, factorial * count, count + 1>();
}
Run Code Online (Sandbox Code Playgroud)

虽然计算值是正确的,但编译器会抛出有关模板溢出的错误.似乎limit变量超出范围(下面0),但它不应该发生,因为0-case由if constexpr(…)语句处理.

所以问题是,我错了,应该预期这种行为,还是编译错误?用GCC 7.1.0编译.

c++ templates constexpr if-constexpr

6
推荐指数
2
解决办法
210
查看次数

用 actor 替换 class 关键字会导致错误

这是我的代码:

class Eapproximator
  var step : F64
  new create(step' :F64) =>
    step = step'

  fun evaluate() :F64 =>
    var total = F64(0)
    var value = F64(1)
    while total < 1 do
      total = total + step
      value = value + (value * step)
    end
    value

actor Main
  new create(env: Env) =>
    var e_approx = Eapproximator(0.00001)
    var e_val = e_approx.evaluate()
    env.out.print(e_val.string())
Run Code Online (Sandbox Code Playgroud)

它运行良好并打印(如预期)2.7183。但是,如果我在定义中替换class为,则会出现一堆错误:actorEapproximator

Error:
/src/main/main.pony:18:34: receiver type is not a subtype of target type
    var e_val = e_approx.evaluate() …
Run Code Online (Sandbox Code Playgroud)

class actor ponylang

3
推荐指数
1
解决办法
127
查看次数

标签 统计

actor ×1

c++ ×1

class ×1

constexpr ×1

if-constexpr ×1

ponylang ×1

templates ×1