views:

1051

answers:

3

Please consider this code:

template<typename T>
char (&f(T[1]))[1];

template<typename T>
char (&f(...))[2];

int main() { char c[sizeof(f<void()>(0)) == 2]; }

I expected it doing SFINAE and chosing the second overload, since substitution of T into T[1] yields

 void [1]()

Which is an invalid type, of course. Adjustment of parameter types (array->pointer) is done after substituting template parameters into function parameters and checking for valid resulting types like 14.8.2 [temp.deduct] describes.

But both comeau and GCC fail to compile the above. Both with different diagnostics.

Comeau says:

"ComeauTest.c", line 2: error: array of functions is not allowed char (&f(T[1]))[1];

GCC says (version 4.3.3):

error: ISO C++ forbids zero-size array c

Meaning, GCC does not fail to substitute, but it chooses the first overload of f, returning a sizeof of 1, instead of failing to substitute it up front like Comeau.

What compiler is right and is my code valid at all? Please refer to or quote the proper Standard section in your answer. Thanks!


Update: The Standard itself contains such an example in the list at 14.8.2/2. I dunno why i overlooked it first:

template <class T> int f(T[5]);
int I = f<int>(0);
int j = f<void>(0); // invalid array

While the example is only informative, it shows the intention of all those mysterious paragraphs and seems to show the code above should work and reject the first overload.

+1  A: 

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.

Eclipse
+1  A: 

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.

avakar
a deduction failure should not however result in a compilation failure. 14.8.3/1 says "If, for a given function template,argument deduction fails, no such function is added to the set of candidate functions for that template.". Nowhere it is stated that such a deduction failure results in a program being ill-formed.
Johannes Schaub - litb
litb, you're right of course.
avakar
It is little unsettling that Comeau should be wrong, though.
avakar
Yeah this is what i think about it too. Looks like comeau fails within 8.3.5 trying to adjust void[1]() to void(*)(), and then noting that void[1]() is an invalid type, not knowing that it is within a type deduction process. If it were to allow to error out at that stage, it would have to error out for other invalid types too, i suspect, like for template<typename T> char ( but there, it's actually causing an SFINAE error (no real diagnostic, but just using the second overload).
Johannes Schaub - litb
+9  A: 

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

I may have a reason for the behaviour of g++. I'm not sure its specified 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 do not see anything in the standard that prohibits the compiler from performing 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 to function, rather than an array of functions.

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

Personally, I think it makes sense to allow the adjustments to happen twice since otherwise it complicates the matching of function template overloads

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

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

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.

Richard Corden
hmm good point about multiple declarations. I've no idea about what is chosen there, actually. Ill have to look for it later. Thanks for your help dude :) I also think now it would be a good idea just to say it first decays (using "T as element/function type"), then substitutes, then decays again.
Johannes Schaub - litb
The more I think of this, the more I feel that it deserves to have a core issue opened for it. I tried some examples with Comeau and it seems to be the first function type which is used to perform the checking always. However, this is not sensible, as it implies that the order in which declarations are seen is important - something that should not be the case.
Richard Corden
Another possibility would be to state that two template declarations that have identical parameter-type-lists are funtionally-equivalent (defined at 14.6.6.1/6 - so far only for expressions, not yet for parameter types). But saying they are equivalent if the non-modified parameter-type-list's are the same. Then, if the declarations of a function template are not equivalent, but functional equivalent, the program is ill-formed; no diagnostic required.
Johannes Schaub - litb
I think you provided an important insight: GCC is correct, since if the declaration used T[1], GCC is allowed to change it to T*, by the rules of 14.5.5.1 [temp.over.link] - but nothing forbids comeau to not change it, since after all both are equivalent - but i think comeau should still not fail hard - but make it a deduction failure. Since this is obviously a contradiction (two equivalent declaration fail one time, but not the other time?), i agree with you that this is a bug in the standard, and accept your stuff. thanks so much for your insight :)
Johannes Schaub - litb