C++ Logo


Advanced search

Thou shalt not... specify deducible template arguments?

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Thu, 29 Oct 2020 13:13:08 +0100

With the adoption of P0551 in C++20 the situation about "What can you do
with Standard Library function templates?" is:

- you can call them (d'uh!);
- you can instantiate them;
- you cannot specialize them;
- you can form pointers to their instantiations, but only if explicitly
- you can overload them for your types, but only if they're
customization points.

Something that you are still authorized to do is to explicitly specify
the template arguments, even when they could be deduced from the
function parameters:

> // This makes sense, no way to know T (but we don't specify Args)
> std::make_unique<Foo>(1, 3.14);
> // This is technically legal, but makes no sense
> std::sort<std::vector<int>::iterator>(v.begin(), v.end());
> // This is also legal, but will break if someone decides to change
> // min's signature (...like it happened with make_pair...)
> std::min<double>(3.14, 1.23f);

Overall, this clashes with SD-8:

> Primarily, the standard reserves the right to:
> Make changes to existing interfaces in a fashion that will be backward compatible, if those interfaces are solely used to instantiate types and invoke functions. Implementation details (the primary name of a type, the implementation details for a function callable) may not be depended upon.
> For example, we may change implementation details for standard function templates so that those become callable function objects. If user code only invokes that callable, the behavior is unchanged.

This gives the Standard Library the right to default template arguments,
change their order, add overloads, etc.; functions are supposed to be
*called*. Should [namespace.std] be further amended to make it explicit
that one must never specify the template arguments for a function
template, if they can be deduced from the function parameters?


Giuseppe D'Angelo | giuseppe.dangelo_at_[hidden] | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - The Qt, C++ and OpenGL Experts

Received on 2020-10-29 07:13:14