itanium-cxx-abi / cxx-abi

C++ ABI Summary
504 stars 94 forks source link

New mangling rule for deduced default template arguments. #184

Open mizvekov opened 5 months ago

mizvekov commented 5 months ago

As part of https://isocpp.org/files/papers/P3310R0.html, we would like to introduce deduction of default template arguments.

When deducing a template template parameter from a template specialization, we propose to default any extra arguments the parameter side would not consume, thus effectively allowing a template template parameter to bind to a template declaration with less template parameters, even if the extra parameters aren't declared as defaulted in the template declaration.

Consider:

template<class T, class U> struct A {};
A<int, float> v;
template<template<class> class TT> void f(TT<int>);

// OK: TT picks 'float' as the default argument for the second parameter.
void g() { f(v); }

Since 'f' specialized from A<int, float> is different from 'f' specialized from A<int, double>, this will affect mangling and so we need new rules.

This should only affect template template arguments.

A class template specialization A<int, double> which was specialized as written, is still the same class when specialized from a template template parameter bound to A deduced from a different specialization.

I would like guidance with such a rule.

My initial idea would be that when mangling such a template template argument, we follow the template name with a number, which is a starting position from which the first argument is deduced, followed by the same mangling we currently use for a template argument list.

Thus:

template<class T, class U> struct A {};

template<template<class> class TT> void f(TT<int>);

void z() {
  f(A<int, double>());
}

This 'f' specialized with A<int, double> would be mangled as _Z1fITtTyE1A1IdEEvT_IiE.

rjmccall commented 4 months ago

Mangling an adjustment on the template argument seems like the right approach. That particular suggestion seems likely to cause problems because of how it separates the template-name from the template-arguments and just drops an arbitrary numeral into the mangling, but we can surely come up with something. I believe we already specify an adjustment for changing template signatures for template template arguments; it's possible that that would be sufficient here, or maybe we need to be more explicit about the deduction.

mizvekov commented 4 months ago

I believe we already specify an adjustment for changing template signatures for template template arguments; it's possible that that would be sufficient here, or maybe we need to be more explicit about the deduction.

Is this about https://github.com/itanium-cxx-abi/cxx-abi/pull/166, wrt the template-param-decl production?

rjmccall commented 4 months ago

Yes. Thanks for reminding me that I need to put some effort into landing that in the spec.

mizvekov commented 4 months ago

It seems that could work, but it needs to be extended to encode default arguments as well.

Though it seems this could already be required for the proposed resolution of https://open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1286