0

In the following example, I have an algorithm depending on another algorithm through a template.

class absolute_distance
{
    public:
        absolute_distance() noexcept {}
        ~absolute_distance() noexcept {}
        double compute(double x1, double x2) const noexcept { return std::abs(x1 - x2); }
};

template <typename D = absolute_distance>
class Algorithm
{
    private:
        D d;
    public:
        Algorithm() : d() {}
        Algorithm(D const& d_) : d(d_) {}
        ~Algorithm() {}
        double execute(double x1, double x2) const { return this->d.compute(x1,x2) + 1.0; }
};

Now, I would like to specify noexcept specifiers on the functions of my class Algorithm. Of course, it depend on the noexcept specifications of the template class D.

For the constructors, it's quite easy, it depend on the constructor of D actually used:

Algorithm() noexcept(noexcept(D())) : d() {}
Algorithm(D const& d_) noexcept(noexcept(D(d_))) : d(d_) {}

For the function execute it seems more complicated... I don't know how to specify the noexcept-ness due the following problem: I can write

double execute(double x1, double x2) const noexcept(noexcept(D().compute(x1,x2))) { return this->d.compute(x1,x2) + 1.0; } 

to check the noexceptness of the function compute used in the function execute, but this checks the function compute AND the constructor of D, and maybe the constructor of D could throw.

Is there a way to only check the noexcept specification of D::compute ?

Caduchon
  • 4,574
  • 4
  • 26
  • 67
  • 1
    This compiles (but I am not sure enough about it to post an answer) `double execute(double x1, double x2) const noexcept(noexcept(d.compute(x1,x2))) { return d.compute(x1,x2) + 1.0; }` - live - https://godbolt.org/z/KGha6TnzT – Richard Critten Jul 14 '21 at 10:34
  • @RichardCritten it works for me too. I tried with `this->d` and it was refused. But I did not tired without `this`. Thanks. – Caduchon Jul 14 '21 at 10:59

0 Answers0