char (&f(...))[2];
This is a declaration of a function that returns a reference to a two-character array. Brackets are syntactically correct for this. Otherwise, & will be bound to char , and there will be a syntax error, since [2] will be meaningless.
The syntax can be decomposed using a type alias. For instance:
using array_ref = char (&)[2]; array_ref f(...);
The reason for returning the reference to the array, and not the actual array, is because arrays cannot be returned from functions. It's impossible. You can only return references or pointers to arrays, as functions.
In all examples ... is a package of C variable arguments .
The only places I've seen this syntax is where it is used as part of the function overload resolution for SFINAE . Typically, this function accompanies an overload with the same name that uses a template replacement to validate an attribute of a given type. If replacement fails, the second standby overload (the one that receives the variational packet) is selected as standby. His type of return is what distinguishes success or failure.
For example, here is a feature class that checks if the type has a member function f() :
template <typename T> struct has_f { private: using true_type = char (&)[1]; using false_type = char (&)[2]; template <typename U> static decltype(std::declval<U>().f(), true_type()) f(int); template <typename> static false_type f(...); public: static constexpr bool value = sizeof(check<T>(0)) == 1; };
As you can see, if T has a member function f() , then the size of the return type will be 1 , otherwise 2 . true_type and false_type these days are largely replaced by the standard attribute classes std::true_type and std::false_type , but this is just an example to illustrate its use.
source share