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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:24:03+00:00 2026-05-23T02:24:03+00:00

I would like to add a function that returns the .size() value as an

  • 0

I would like to add a function that returns the .size() value as an integer, instead of unsigned integer.

Edit: Due to comments, i explain more detailed:

I have code:

int something = 3;
if(arr.size() > something) 

Which will produce compiler warning, and i dislike adding (int) to every place where i have this.
So, a solution i thought it would be nice to have sizei() function:

int something = 3;
if(arr.sizei() > something) 

Which wouldnt produce a warning.

So, im not wanting to create a separate function, but a function in the std::vector itself.

Edit: Seems like the only way to do this is to create another function, such as:

template <typename T>
inline int sizei(const T &arr){
    return (int)arr.size();
}

On the positive side: this doesnt seem to increase my executable size at all.

  • 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-23T02:24:03+00:00Added an answer on May 23, 2026 at 2:24 am

    As a rule, in C and C++ you should never use an unsigned type such as size_t to restrict the domain. That’s because (1) these languages provide no range checking, and (2) they do provide unreasonable implicit promotions. No range checking means (1) no advantage, and unreasonable implicit promotions means (2) very undesirable disadvantages, so it’s plain stupid thing to do: no advantage, very undesirable disadvantages.

    However, the standard libraries for these languages do that. They do it for historical reasons only, caught irreversibly in early decisions which at one time made sense. This has both extremely silly consequences such as C99 requiring 17 (!) bits for ptrdiff_t, and it has the aforementioned extremely undesirable consequences such as using inordinately much time on hunting down bugs resulting from implicit promotions (etc.). For example, in C++ you are practically guaranteed that std::string( "bah!" ).length() < -5 – which can easily trip you up and anyway is as silly as it is possible to design.

    Now, you can’t infuse new member functions in std::vector, but you can add a freestanding function. A good name is countOf. Template it so that it can be applied to just about anything (raw arrays, vectors, etc.).

    The triad of functions startOf, endOf and countOf were, as far as I know, first identified by Dietmar Kuehl. C++0x will have std::begin and std::end, but AFAIK no corresponding std::size. In the meantime you can just define this support, which allows you to treat any kinds of container plus raw arrays the same.

    An example implementation & further discussion is provided at my blog.


    EDIT Adding some code, because it’s requested in the comments.

    Detection of suitable iterator type:

    template< typename Type >
    struct It
    {
        typedef typename Type::iterator T;
    };
    
    template< typename Type >
    struct It< Type const >
    {
        typedef typename Type::const_iterator T;
    };
    
    template< typename ElemType, Size N >
    struct It< ElemType[N] >
    {
        typedef ElemType* T;
    };
    

    And the countOf, startOf and endOf functions, using that deduced iterator type:

    template< typename T >
    inline Size countOf( T const& c )           { return static_cast<Size>( c.size() ); }
    
    template< typename T, Size N >
    inline Size countOf( T (&)[N] )             { return N; }
    
    template< typename T >
    inline typename It<T>::T startOf( T& c )    { return c.begin(); }
    
    template< typename T, Size N >
    inline T* startOf( T (&a)[N] )              { return a; }
    
    template< typename T >
    inline typename It<T>::T endOf( T& c )      { return c.end(); }
    
    template< typename T, Size N >
    inline T* endOf( T (&a)[N] )                { return a + N; }
    

    where Size is a typedef for ptrdiff_t.

    Note: in 64-bit Windows int (and even long) is 32-bit. Hence, int is in general not sufficient for a really large array. ptrdiff_t is guaranteed to be able to represent the difference between any two pointers, when that difference is well-defined.


    Cheers & hth.

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

Sidebar

Related Questions

I have a function that returns an integer , however I would like to
I would like to add some code to my Application.cfc onRequestEnd function that, if
I have a function that returns a 2 dimensional array. Due to the nature
I have a class that contains some data, and I would like to add
I would like to do something like add a nice-to-Excel-functions Name property to the
I would like to add some debugs for my simple ruby functions and I
I would like to add the following MIME type to a site run by
I would like to add a DataGridViewTextBoxCell cell to a DataGridViewCell control, but as
I would like to add AES encryption to a software product, but am concerned
I would like to add graphing to my User Controls in ASP.NET MVC. I

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.