Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 211313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:05:03+00:00 2026-05-11T18:05:03+00:00

Please consider this code: template<typename T> char (&f(T[1]))[1]; template<typename T> char (&f(…))[2]; int main()

  • 0

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 don’t know, 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:05:03+00:00Added an answer on May 11, 2026 at 6:05 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider this code: #include <iostream> template<typename T> void f(T x) { std::cout <<
Please consider the following code: #include <iostream> #include <typeinfo> template< typename Type > void
please consider the following code: template <typename T> struct foo { template <typename S>
Consider the following code: struct Foo { mutable int m; template<int Foo::* member> void
Consider the following code template<typename T, int N> struct A { typedef T value_type;
Please consider this code: System.Linq.Expressions.Expression<Func<tbl, bool>> exp_details = r => r.ID_Master == Id &&
Please consider the following piece of code: int main() { typedef boost::ptr_vector<int> ptr_vector; ptr_vector
Please consider this code: trait A { def a : Int } def f
Please consider the following code: vector<int> myVector; myVector.push_back(10); myVector.erase(myVector.end()); This code compiles and runs
Please, consider the code below: template<typename T> bool function1(T some_var) { return true; }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.