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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:09:31+00:00 2026-06-05T08:09:31+00:00

The code below demonstrates a behavior of gcc 4.6.2 that I can’t account for.

  • 0

The code below demonstrates a behavior of gcc 4.6.2 that I can’t account for. The first function declares a static array of type vec_t, where vec_t is a typedef’d alias for unsigned char. The second function is identical, except that the type of vect_t is a template parameter. The second function fails to compile with diagnostic “error: storage size of ‘bitVec’ isn’t constant”.

#include <limits>

void bitvec_func()
{
    const std::size_t       nbits = 1e7;
    typedef unsigned char   vec_t;
    const std::size_t       WLEN  = std::numeric_limits<vec_t>::digits;
    const std::size_t       VSIZ  = nbits/WLEN+1;
    static vec_t            bitVec[nbits/VSIZ];    // Compiles fine
}

template <typename T>
void bitvec_func()
{
    const std::size_t       nbits = 1e7;
    typedef T               vec_t;
    const std::size_t       WLEN  = std::numeric_limits<vec_t>::digits;
    const std::size_t       VSIZ  = nbits/WLEN+1;
    static vec_t            bitVec[nbits/VSIZ];    // "error: storage size of ‘bitVec’ isn’t constant"
}

void flarf()
{
    bitvec_func();
    bitvec_func<unsigned char>();
}

It seems to me that instantiating the template with argument <unsigned char> should cause the compiler to generate the same code as the first function. Can anyone offer any insight into why this does not seem to be the case?

[Addendum: the second function will compile with “-std=c++0x” or “-std=gnu++0x”, but I’d still like to understand how/if it’s wrong under the earlier language definitions.]

ETA:
The second function will compile if the initializer for nbits is changed:

const std::size_t       nbits = 1e7;              // Error
const std::size_t       nbits = (std::size_t)1e7; // Okay
const std::size_t       nbits = 10000000.0;       // Error
const std::size_t       nbits = 10000000;         // Okay

In other words, it seems that if nbits is initialized with an expression of an integral type, then nbits is treated as a constant in the definition of bitVec. If nbits is instead initialized with a floating-point expression, the compiler no longer sees it as constant in the expression for the dimension of bitVec, and the compilation fails.

I’m a lot less comfortable calling “compiler bug” in C++ than I would be in C, but I can’t think of any other reason that the above 4 cases would not be semantically identical. Anyone else care to opine?

  • 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-06-05T08:09:32+00:00Added an answer on June 5, 2026 at 8:09 am

    After compiling your code with -ansi on gcc 4.7.0, I was able to reproduce this warning:

    warning: ISO C++ forbids variable length array 'bitVec' [-Wvla]
    

    This warning appeared for both bitVec, not just the one in the template function. I then realized that the line nbits = 1e7; is assigning a double to an unsigned int. I think because of this, for some reason causes nbits to not be a constant expression. The reason your code is compiling for the non-templated version is because of the variable length array extension for gcc. Also, your version of gcc for some reason doesn’t allow variable length arrays in function templates. To fix your code change 1e7; to 10000000.

    EDIT

    I asked another question concerning the rule. The answer is in C++03 the code is invalid, but in C++11 it is okay.

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

Sidebar

Related Questions

Below, is some toy code that demonstrates my question. The first one is a
Below is a simple code snippet that demonstrates the seemingly buggy behavior of end
The code below working for only first div. I can't display other divs when
I've run into a rather interesting problem. The code below generates an array that,
The code below demonstrates the problematic joda-time implementation of week calculation. This behavior is
The code below doesn't seem to work or find anything on an array. I'm
The code below removes www., etc. from the beginning of websites that are entered
The code below demonstrates this difference: #include <iostream> #include <string> int main() { char
The code below demonstrates this #include <float.h> #include <stdint.h> #pragma fenv_access(on) // apparently ineffective
Below is a small test case that demonstrates a problem that I am trying

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.