Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's so controversial about AFTs (Abbreviated Function Templates)?

Why is this feature seen so controversial? I saw that it was not merged into C++20 along with the rest of the Concepts TS. Searching through the web, I couldn't find reasonable arguments except the generic ones that can be applied to almost any new C++ feature.

What are people so afraid of? What pitfalls could be in there? After all, we already have generic lambdas.

like image 866
GreenScape Avatar asked Aug 21 '18 17:08

GreenScape


1 Answers

Consider the following program:

void foo(Name&& x) {
    static int counter = 0;
    ++counter;
    x.func<int>(); // #1
}

Name x = ...;
foo(x); // #2

template <typename T> void some_algorithm(T);
some_algorithm(foo); // #3

If Name is a type, there is exactly one static variable named counter, foo is a single function that takes an rvalue reference, the call in #1 is fine (presuming such a member function exists), the call in #2 is ill-formed because x is an lvalue, and the call in #3 is well-formed because foo just decays to a function pointer.

If Name is a concept, there is one static variable per type instantiated (including different value categories), foo is a function template that can only be defined in a header, the call in #1 is ill-formed because you need an extra template keyword, the call in #2 is well-formed because we just deduce an lvalue reference parameter, and the call to #3 is ill-formed because you can't pass in a function template like that - you need to wrap it in a lambda or something.

This stacks on each parameter:

void foo(Name&&, OtherName&&);

Either name could be a type or a concept without a marker.

All based on what category of thing Name is. That's... a lot of difference (mind you the above is also not a complete list of the differences, just the biggest ones I can immediately think of), and there is concern that it is too much without commensurate benefit.

See also P0696.


Note that none of this applies with generic lambdas and auto because auto is a keyword, and so void foo(auto&& x) would clearly be a function template. It's a clear marker that avoids all of this ambiguity.

Also note that P1141 proposes using that same syntactic marker for AFTs:

void f(Sortable auto& x);

Given that seemingly everyone who has loudly voiced one opinion or other on this topic is a coauthor of that paper, I'd give it a better than average chance of success.

like image 174
Barry Avatar answered Sep 23 '22 23:09

Barry