jimifiki
jimifiki

Reputation: 5544

C++ noexcept specification depending on data members

this declaration is ok:

void memberFunction(T& functor, double value)noexcept(noexcept(functor(value))); 

for a

template<class T> 
class MyClass{
    public: 
        void memberFunction(T& functor, double value)noexcept(noexcept(functor(value))); 
};

Let's say that MyClass has a functor data member:

template<class T> 
class MyClass{
    public: 
        //ctor 
        ... 
        void memberFunction(double value); 
    private: 
        T functor; 
};

I want to write the noexcept specification as I did in the previous case, I tried this:

void memberFunction(double value)noexcept(noexcept(functor(value))); 

but the compiler tells me that functor is not the scope. The following doesn't work for analogous reasons:

void memberFunction(double value)noexcept(noexcept(this->functor(value))); 

And the following can't work because I have some classes used as T which lacks a default constructor:

void memberFunction(double value)noexcept(noexcept(T()(value))); 

The following is syntatically wrong:

void memberFunction(double value)noexcept(noexcept(T::operator(double))); 

eve though it pictorically explains well what I want.

Any suggestion? For the moment I gave up with the specification...

Upvotes: 1

Views: 152

Answers (2)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275800

std::declval<T>() simulates an rvalue T instance. std::declval<T&>() simulates an lvalue T instance. Use this in place of T().

Upvotes: 2

TartanLlama
TartanLlama

Reputation: 65720

That noexcept specification is part of the declaration for memberFunction and therefore doesn't have access to any member data declared afterwards.

The simple fix is to move the declaration of functor above memberFunction:

template<class T> 
class MyClass{
    private: 
        T functor; 
    public: 
        //ctor 
        ... 
        void memberFunction(double value); 
};

Upvotes: 2

Related Questions