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 109683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:06:29+00:00 2026-05-11T02:06:29+00:00

My compiler behaves oddly when I try to pass a fixed-size array to a

  • 0

My compiler behaves oddly when I try to pass a fixed-size array to a template function. The code looks as follows:

#include <algorithm> #include <iostream> #include <iterator>  template <typename TSize, TSize N> void f(TSize (& array)[N]) {     std::copy(array, array + N, std::ostream_iterator<TSize>(std::cout, ' '));     std::cout << std::endl; }  int main() {     int x[] = { 1, 2, 3, 4, 5 };     unsigned int y[] = { 1, 2, 3, 4, 5 };     f(x);     f(y); //line 15 (see the error message) } 

It produces the following compile error in GCC 4.1.2:

test.cpp|15| error: size of array has non-integral type ‘TSize’ test.cpp|15| error: invalid initialization of reference of type   ‘unsigned int (&)[1]’ from expression of type ‘unsigned int [5]’ test.cpp|6| error: in passing argument 1 of ‘void f(TSize (&)[N])   [with TSize = unsigned int, TSize N = ((TSize)5)]’ 

Note that the first call compiles and succeeds. This seems to imply that while int is integral, unsigned int isn’t.

However, if I change the declaration of my above function template to

template <typename TSize, unsigned int N> void f(TSize (& array)[N]) 

the problem just goes away! Notice that the only change here is from TSize N to unsigned int N.

Section [dcl.type.simple] in the final draft ISO/IEC FDIS 14882:1998 seems to imply that an ‘integral type’ is either signed or unsigned:

The signed specifier forces char objects and bit-fields to be signed; it is redundant with other integral types.

Regarding fixed-size array declarations, the draft says [dcl.array]:

If the constant-expression (expr.const) is present, it shall be an integral constant expression and its value shall be greater than zero.

So why does my code work with an explicit unsigned size type, with an inferred signed size type but not with an inferred unsigned size type?

EDIT Serge wants to know where I’d need the first version. First, this code example is obviously simplified. My real code is a bit more elaborate. The array is actually an array of indices/offsets in another array. So, logically, the type of the array should be the same as its size type for maximum correctness. Otherwise, I might get a type mismatch (e.g. between unsigned int and std::size_t). Admittedly, this shouldn’t be a problem in practice since the compiler implicitly converts to the larger of the two types.

EDIT 2 I stand corrected (thanks, litb): size and offset are of course logically different types, and offsets into C arrays in particular are of type std::ptrdiff_t.

  • 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. 2026-05-11T02:06:30+00:00Added an answer on May 11, 2026 at 2:06 am

    Hmm, the Standard says in 14.8.2.4 / 15:

    If, in the declaration of a function template with a non-type template-parameter, the non-type template-parameter is used in an expression in the function parameter-list and, if the corresponding template-argument is deduced, the template-argument type shall match the type of the template-parameter exactly, except that a template-argument deduced from an array bound may be of any integral type.

    Providing this example:

    template<int i> class A { /* ... */ }; template<short s> void f(A<s>); void k1() {     A<1> a;     f(a);    // error: deduction fails for conversion from int to short     f<1>(a); // OK } 

    That suggests that the compilers that fail to compile your code (apparently GCC and Digital Mars) do it wrong. I tested the code with Comeau, and it compiles your code fine. I don’t think there is a different to whether the type of the non-type template parameter depends on the type of the type-parameter or not. 14.8.2.4/2 says the template arguments should be deduced independent from each other, and then combined into the type of the function-parameter. Combined with /15, which allows the type of the dimension to be of different integral type, i think your code is all fine. As always, i take the c++-is-complicated-so-i-may-be-wrong card 🙂

    Update: I’ve looked into the passage in GCC where it spits out that error message:

      ...   type = TREE_TYPE (size);   /* The array bound must be an integer type.  */   if (!dependent_type_p (type) && !INTEGRAL_TYPE_P (type))     {       if (name)     error ('size of array %qD has non-integral type %qT', name, type);       else     error ('size of array has non-integral type %qT', type);       size = integer_one_node;       type = TREE_TYPE (size);     }   ... 

    It seems to have missed to mark the type of the size as dependent in an earlier code block. As that type is a template parameter, it is a dependent type (see 14.6.2.1).

    Update: GCC developers fixed it: Bug #38950

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

Sidebar

Ask A Question

Stats

  • Questions 64k
  • Answers 64k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer It sounds like you're looking for urllib. Here's an example… May 11, 2026 at 10:51 am
  • added an answer I've seen quite a lot of examples that use a… May 11, 2026 at 10:51 am
  • added an answer The catch is that fd_set is not really a 'set'… May 11, 2026 at 10:51 am

Related Questions

My compiler behaves oddly when I try to pass a fixed-size array to a
My compiler (VC++ 6.0 sp6) has apparently gone insane. In certain pieces of code
I'm having a problem with my compiler telling me there is an 'undefined reference
I'm a Java programmer, and I like my compiler, static analysis tools and unit
I'm fine working on Linux using gcc as my C compiler but would like
In my independent study of various compiler books and web sites, I am learning
My day job includes working to develop a Pascal-like compiler. I've been working all
I talked my team into turning on compiler warnings again. Some how all warnings
I realize that far is compiler specific, but my expectation is that the placement
I am running the C# compiler in code, sort of my own IDE. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.