On 14/05/2026 10:01,
boltar@caprica.universe wrote:
On Wed, 13 May 2026 10:17:32 +0200
David Brown <david.brown@hesbynett.no> gabbled:
On 13/05/2026 09:55, boltar@caprica.universe wrote:
template<typename T1, typename T2>
X add(T1 a, T2 b)
{
˙˙˙˙return a + b;
}
What type should X be?˙ It depends on the types of "a" and "b" (which
might be class types here).˙ So you can write:
template<typename T1, typename T2>
auto add(T1 a, T2 b) -> decltype(a + b)
{
˙˙˙˙return a + b;
}
A sane syntax would have been
template<typename T1, typename T2>
(T1 + T2) add(T1 a, T2 b)
{
:
}
But this is C++ so not a chance, lets make it as verbose as possible.
You might also use it for complicated types, such as returning a
function pointer, where it is clearer:
int (*get_function_pointer(int x))(int);
auto get_func_pointer(int x) -> int(*)(int);
I'll give you that, but the C function pointer syntax was unnecessarily
messy and there's no reason C++ couldn't have tidied it up even before std::function came along. Something like:
int (*(int))get_func_pointer(x)
{
:
}
I don't think anyone sees the C or C++ type syntaxes as being the nicest conceivable for a programming language. But the languages are what they
are, stretching back to a time when the syntax choices were made as good design decisions. Modern C++ cannot start from scratch - new features
have to fit with the existing language, existing tools, existing
programmers, and existing code. Sometimes things can be "tidied up" a
bit - that's exactly the case for trailing return types. (It might have
been nicer if a new keyword - say, "func" - had been introduced to mean exactly the same as "auto" but only available for function declarations.
But new keywords are not free in an established language.)
--- PyGate Linux v1.5.14
* Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)