mercredi 14 juillet 2021

noexcept specifier depending on a template class member function

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 ?

Aucun commentaire:

Enregistrer un commentaire