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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:33:27+00:00 2026-06-13T03:33:27+00:00

I was thinking to be more pedantic in choosing data type in a block

  • 0

I was thinking to be more pedantic in choosing data type in a block of code where I would need to choose between size_type size_t in general or container::size_type for container types. My problem is that if I have the following block of code, I don’t know how to do it. Can anybody there help?

template<typename some_container>
int func(some_container& input)
{
    //Some code...
    //...
    decltype(input.size()) variable_x; //Choose this if defined,
    size_t                 variable_x; //otherwise choose this
    //... Some more code...
}

In this case some_container may be a custom container and does not provide size() function. What led me to this thinking was reading the difference between size_t and container::size_type at size_t vs container::size_type. I also read Determine if a type is an STL container at compile time, but the approach feels a bit heavy-handed for my situation.

  • 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-13T03:33:29+00:00Added an answer on June 13, 2026 at 3:33 am

    Following is one way to determine if a class contains a type (e.g. size_type) or not:

    template <typename T> 
    struct Has_size_type
    {
      typedef char (&yes)[2];
    
      template <typename C> static yes test(typename C::size_type*);
      template <typename> static char test(...);
    
      static const bool value = sizeof(test<T>(0)) == sizeof(yes);
    };
    

    And following is the way to choose between 2 types:

    template<bool> struct Bool;
    template<typename T, typename = Bool<true> >
    struct Set { typedef size_type type; };
    template<typename T>
    struct Set<T,Bool<Has_size_type<T>::value> > { typedef typename T::size_type type; };
    

    Edit start: Here is another simpler approach:

    template<typename T>
    struct void_ { typedef void type; };
    
    template<typename T, typename = void>
    struct Set 
    { typedef size_type type; };
    
    template<typename T>
    struct Set<T,typename void_<typename T::size_type>::type>
    { typedef typename T::size_type type; };
    

    Edit end.

    So finally, use as below:

    template<typename some_container>
    int func(some_container& input)
    {
      typedef typename Set<some_container>::type type;
    }
    

    So now type is either size_type or some_container::size_type, if it has that.

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

Sidebar

Related Questions

Maybe I am just over thinking this and need to write some more prototype
This is more thinker than problem. I have a problem with thinking about how
As I am learning more about rails, and breaking my design thinking from ASP.Net
I'm thinking that google is fall down with layout for these phone. is more
Thinking about avoiding code replication, I got a question that catches me every time
Thinking about if we modify the definition of Hamiltonian path as we need to
I have multiple web applications running in CloudBees run@cloud. I need to communicate between
Thinking that the answer to this is pretty obvious but here it goes: When
Im thinking about to use a DB for my logs instead of a normal
Im thinking about building an application on our website to track order places through

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.