0

I wanted to see if I can deduce the template parameters of a member class object in the member initializer list. I suppose this is not (yet) possible. Am I right in assuming so?

Demo

#include <cstdio>

template <typename Callable>
struct some_struct
{
    some_struct(Callable fn)
    :   fn_{fn}
    {

    }

    Callable fn_;
};

struct another_struct
{
    another_struct()
        :   ss_([this](){ this->print(); }) // <- this should deduce 'Callable'
    { }

    auto print() -> void
    {
        printf("Hello World!\n");
    }

    some_struct ss_; // no template arguments passed here though..
};

int main()
{
    another_struct a;
}

The compiler doesn't like it but I could also be doing it wrong..

<source>:26:5: error: invalid use of template-name 'some_struct' without an argument list
   26 |     some_struct ss_;
      |     ^~~~~~~~~~~
glades
  • 3,778
  • 1
  • 12
  • 34

0 Answers0