P0091R3 ("Template argument deduction for class templates") was recently added to gcc
trunk and can be tested on wandbox.
Something I had in mind was the possibility of using it to implement a "scope guard" in very few lines of code:
scope_guard _([]{ cout << "hi!\n" });
I tried implementing it on wandbox...
template <typename TF>
struct scope_guard : TF
{
scope_guard(TF f) : TF{f} { }
~scope_guard() { (*this)(); }
};
int main()
{
scope_guard _{[]{}};
}
...but compilation failed with the following error:
prog.cc:6:5: error: 'scope_guard(TF)-> scope_guard<TF> [with TF = main()::<lambda()>]', declared using local type 'main()::<lambda()>', is used but never defined [-fpermissive]
scope_guard(TF f) : TF{std::move(f)} { }
^~~~~~~~~~~
I then tried using a non-lambda local type, and got the same error.
int main()
{
struct K { void operator()() {} };
scope_guard _{K{}};
}
Afterwards, I tried a non-local type, and it worked as expected.
struct K { void operator()() {} };
int main()
{
scope_guard _{K{}};
}
Is this featured designed in such a way that prevents local types from being deduced?
Or is this a defect in gcc
's current implementation of the feature?