How to enable automatic type deduction for class specific typedefs (c++17)?

Viewed 63

I would like to deduce a class' (with default template parameter) typedef with c++17 automatically. Does somebody know if this is possible? The following code tries to illustrate this:

#include <vector>
template <typename T = int>
struct A{
    using Vec = std::vector<T>;
};

int main() {
    A a{}; // works with c++ 17
    A<int>::Vec vec2{}; //works
    A::Vec vec{}; //does not seem to work with c++ 17. Is that possible somehow?
}

In the code above, a is instantiated without problems, and the deduced template type is int as per default. Nevertheless, I have to pass the parameter type to use the typedef Vec. I would be very grateful if somebody knew an answer to the two following questions:

  • What is the paper number/title of the ISO mailing list for the feature allowing automatic type deduction for A a{}? I am curious to learn more about this feature.
  • Is it possible to infer the type of Vec automatically without specifying the template type of A explicitly?

I am grateful for any hints or suggestions!

2 Answers

Is it possible to infer the type of Vec automatically without specifying the template type of A explicitly?

Yes. You can leave the <> empty:

A<>::Vec vec{};

Otherwise, A without <> or <int> etc., names a template with an unknown template type parameter, and you cannot use that to access nested identifier.

What is the paper number/title of the ISO mailing list for the feature allowing automatic type deduction for A a{}? I am curious to learn more about this feature.

P1814 is the main paper on class template argument deduction ("CTAD"). Here is the page on cpprefernece. Note that A<>::Vec vec{} does not rely on CTAD, as there is no contstructor involved that performs an actual deduction.

Is it possible to infer the type of Vec automatically without specifying the template type of A explicitly?

Yes:

A<>::Vec
Related