SFINAE with invalid function-type or array-type parameters? SFINAE with invalid function-type or array-type parameters? arrays arrays

SFINAE with invalid function-type or array-type parameters?


A small note, although very rare, I have found some occasions where Ibelieve that the Comeau compiler has it wrong - although, theseoccasions are so rare that its always worth double and triplechecking your assumptions!

I may have a reason for the behaviour of g++. I'm not sure itsspecified exactly when parameter types are adjusted:

Consider the following:

template<typename T>struct A{  void bar (T[10]);};template<typename T>void A<T>::bar (T*){}

The definition of 'bar' is legal, as "T[10]" decays to "T*". I donot see anything in the standard that prohibits the compiler fromperforming the adjustments of 8.3.5 against the template declaration,and it also improves performance when it comes to overload matching.

Applying this to your example, g++ might be treating it as:

template<typename T>char (&f( T* ))[1];template<typename T>char (&f(...))[2];int main() { char c[sizeof(f<void()>(0)) == 2]; }

In the above, the substituted parameter is a legal pointer tofunction, rather than an array of functions.

So, the question for me is - is if there is something that prohibtsthe adjustments for the function parameters (8.3.5) twice?

Personally, I think it makes sense to allow the adjustments to happentwice since otherwise it complicates the matching of function templateoverloads

In conclusion, I think its valid for g++ to select the first overloadbased on how it treates decaying array parameters, and Comeau is wrongnot to have a deduction failure for the array of functions.

Of course this now means that (if Comeau was fixed) then each compilerwould choose a different overload and would still be standardscompliant! :(

EDIT:

Just to illustrate my point, consider the following code:

template <typename T> void foo ( T * );template <typename T> void foo ( T * const );template <typename T> void foo ( T [] );template <typename T> void foo ( T [10] );template <typename T> void foo ( T [100] );void bar () {  foo < void() > ( 0 );}

Here, foo has been declared and redeclared several times. Which declaration, and so which parameter type, should the compiler apply the rules listed in 14.8.2?

My point is that the standard doesn't say anything about the above. I would also go as far as to say that any wording on this would have to leave it as either "undefined" or "implementation defined" behaviour.


Suprisingly enough - this does work in VS2008. I don't think that's necessarily evidence for it being correct behaviour or not though...

Visual Studio is interpretting

char (&f(T[1]))[1];

as a function that takes an array of size 1 of T, and returns a reference to an array of chars of size 1.


I'll try to describe the process of template argument deduction as I understand it from reading the standard.

  1. Explicit template arguments are checked as described in 14.8.2/2.
  2. The resulting function signature is adjusted as per 8.3.5 (i.e. array to pointer decay is performed).
  3. Implicit template arguments are deduced as per 14.8.2.1 (this is performed on a partially substituted signature from step 2).

The deduction for the first overload fails in step 1, the overload resolution therefore returns the second overload. I don't believe the program is ill-formed.