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

  • SEARCH
  • Home
  • 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 6596105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:01:52+00:00 2026-05-25T18:01:52+00:00

There is a class I’m refactoring which currently has a method: void resize(size_t sz)

  • 0

There is a class I’m refactoring which currently has a method:

void resize(size_t sz)

In the current codebase, sz is always 0,1,2,or 3.
The underlying class is changing from dynamic allocation to a preallocated array of maxsize==3.

How can I get a build time error if someone tries to resize to sz>3 ?
It is easy enough to add a runtime check. But I’d rather get a compile-time check that fails sooner.

I don’t want to change any existing code that makes calls with an integer literal that is in-bounds ,e.g.:

x.resize(2)

should still compile as is.

But if someone comes along and tries to

x.resize(4)
  or
x.resize(n)

it should fail to compile or link.

I was thinking about a template specialized on int that is undefined for anything other than {0,1,2,3}. But I’m not sure quite how to make it do what I want within the confines of standard c++.

edit:

I should elaborate on my thoughts of using the template. I am quite willing to change the declaration of the resize function. I am not willing to change the calling code.

e.g. I was thinking something like

void resize( ConstructedFromLiteral<0,3> sz)

or

void resize( ConstructedFromLiteral<0> sz)
void resize( ConstructedFromLiteral<1> sz)
void resize( ConstructedFromLiteral<2> sz)
void resize( ConstructedFromLiteral<3> sz)
  • 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-25T18:01:53+00:00Added an answer on May 25, 2026 at 6:01 pm

    There’s no way you can get a compile-time check for a run-time value. Imagine if you said,

    resize(read_some_number_from_disk());
    

    How is the compiler supposed to check that?

    However, you can make the function a template, since template parameters are known at compile time:

    class Foo
    {
      template <unsigned int N> void resize()
      {
        static_assert(N < 4, "Error!");
        //...
      }
    
      //...
    
    };
    

    If you don’t have static asserts, you can rig up your own static-assert class that’ll fail to compile:

    template <bool> struct ecstatic_assert;  // no definition!
    template <> struct ecstatic_assert<true> { } ;
    

    Usage:

    ... resize ... { ecstatic_assert<N < 4> ignore_me; /* ... */ } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Testclass1 has a variable, there is some execution which will change value of
Is there a __CLASS__ macro in C++ which gives the class name similar to
Is there a class for AT communication with devices? Like a class which encapsulates
in eclipse plugin i have two plug-in which are used there own class loader
Hi to all, Is there any class which have combined functionality of ScaleGestureDetector and
Is there any class, library or some piece of code which will help me
There exists interface Algorithm There exists class MathAlgorythm implements Algorithm (returns MathResult, which implements
In a service class there is a method to start the service. If that
I was wondering whether there is class or something similar which I can include
class Article < ActiveRecord::Base has_many :comments belongs_to :category end Is there a class method

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.