user3716072
user3716072

Reputation: 197

Specifying deduction guides for template classes with function pointers

I have the following template class:

double* createSomething(){return new double{};}
template<typename T>
class B {
    public:
    using BuilderFcnT = T (*)();
    explicit B(BuilderFcnT a) : ab(a) {}

    private:
    BuilderFcnT ab;
    
};
//This did not work:
//template<typename T>  B(typename B<T>::BuilderFcnT) -> B<typename std::__invoke_result<typename B<T>::BuilderFcnT>::type>;

int main(){
    B<double*> b{&createSomething};
    //B b{&createSomething}; <- I would like this 
    return 0;
}

I would like the user to use it as follows (I would like to avoid specifying the type and have the compiler deduce it):

B b{&createSomething};//createSomething takes no arguments and returns a pointer to an object

But I am getting warning in Clang compiler with the flag -Wctad-maybe-unsupported , and a warning in gcc. It says that I should specify deduction guides for the template class. I tried the following:

template<typename T>  B(typename B<T>::BuilderFcnT) -> B<typename std::__invoke_result<typename B<T>::BuilderFcnT>::type>;

But I cannot get it to parse. Is there something trivial that I am missing?

Upvotes: 1

Views: 209

Answers (1)

NathanOliver
NathanOliver

Reputation: 180805

The issue with B(typename B<T>::BuilderFcnT) is you are trying to use a type that depends on T, but you are in the process of deducing T. It's like the chicken and egg problem. In this case you just need

template<typename T>  B(T(*)()) -> B<T>;

Upvotes: 3

Related Questions