2

Why the following class A can't deduce its template parameters in the code below:

#include <functional>

template <class... Ts>
class A
{
public:

    using Func = std::function<void(std::decay_t<Ts>...)>;

    A(Func func) : m_func(func)
    {
    }

private:

    Func m_func;
};

int main()
{
    //compiles
    A<int, bool> a([](int, bool) {});

    //does not compile with error 'class template argument deduction failed'
    A b([](int, bool) {});

    return 0;
}

The class have a data member of type Func and needs to know the types of its parameters. How to make it compile?

EDIT1:

I was able to make this compile with std::tuple:

#include <tuple>

template <class... Ts>
class A
{
public:

    using Func = std::tuple<Ts...>;

    constexpr A(Func func) : m_func(func)
    {
    }

private:

    Func m_func;
};

int main()
{
    std::tuple<int, bool> t;
    
    //compiles
    A<int, bool> a(t);

    //do not compile with error 'class template argument deduction failed'
    A b(t);

    return 0;
}
6
  • 2
    Would you believe that this issue is called "non-deducible context'? Your template instantiation needs to deduce a type that after the transformation via std::decay_t results in the type used in the template instantiation. That's not deducible. And since a lambda is just an anonymous class, there is no way to deduce something like that, either. Jan 18, 2021 at 16:44
  • @SamVarshavchik I think std::function's deduction guide could get somewhere
    – Caleth
    Jan 18, 2021 at 16:48
  • @SamVarshavchik yes, std::tuple is better than std::function, see EDIT1, removing std::decay_t does not help.
    – Dmitriano
    Jan 18, 2021 at 16:49
  • That's good news, but what exactly is your question, then? And, as I mentioned, there's also the lambda becoming an anonymous closure, too. Jan 18, 2021 at 16:50
  • CTAD relies on deduction guides. And those deduce stuff like good old function templates always did. And the issue of deducing a std::function from a lambda has been re-hashed already on SO. For example stackoverflow.com/questions/53326206/… Jan 18, 2021 at 16:55

2 Answers 2

1

Two issues.

  1. Implicit conversion (from lambda to std::function) won't be considered in template argument deduction; which causes deduction failing.

  2. The existence of std::decay_t results in non-deduced context; then Ts can't be deduced.

The following code compiles.

#include <functional>

template <class... Ts>
class A
{
public:

    using Func = std::function<void(Ts...)>;

    A(Func func) : m_func(func)
    {
    }

private:

    Func m_func;
};

int main()
{
    //compiles
    A<int, bool> a([](int, bool) {});

    //compiles
    A b(std::function([](int, bool) {}));

    return 0;
}
7
  • std::function([](int, bool) {}) does not compile as it can't deduce template arguments of std::function. Jan 18, 2021 at 17:11
  • @AndreySemashev Why not? CTAD works for it. Jan 18, 2021 at 17:12
  • Also, your second point about non-deduced context is incorrect, since at the point where std::decay_t is used there is no template argument deduction in effect in the first place. Jan 18, 2021 at 17:13
  • @AndreySemashev Sorry I can't get what you mean; if not remove the part of std::decay_t the code won't compile even passing an std::function explicitly to the constructor . You meant the usage of std::decay_t causes not compiling for other reasons? Jan 18, 2021 at 17:15
  • @songyuanyao Hmm, I stand corrected and surprised then. I have no idea how std::function is able to deduce its template arguments. Jan 18, 2021 at 17:17
0

Template parameter deduction traditionally was an exclusive feature of a function call expression. Template parameters of a class template could not be deduced from a constructor call until the introduction of deduction guides in C++17. With that, you can explicitly specify how template parameters of a class should be deduced from a constructor call.

template <class... Ts>
class A
{
public:

    using Func = std::tuple<Ts...>;

    constexpr A(Func func) : m_func(func)
    {
    }

private:

    Func m_func;
};

template< class... Ts >
A(std::tuple< Ts... >) -> A< Ts... >;

int main()
{
    std::tuple<int, bool> t;
    
    A<int, bool> a(t);

    A b(t); // deduces int, bool types from t

    return 0;
}

The use of a deduction guide to deduce a function call signature from a lambda can be problematic. The lambda function does not define a type that corresponds to its signature. If the lambda is polymorphic (i.e. uses auto in its argument list) then such type is not possible to define in the first place. And a function can be called with arguments different from its signature, so there may be multiple different sets of argument types, with which the function is callable. You will most likely have to specify the parameter types explicitly in this case.

Deducing template parameters from function pointers is still possible, though:

template< class R, class... Ts >
A(R (*)(Ts...)) -> A< Ts... >;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.