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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:14:05+00:00 2026-06-01T08:14:05+00:00

If I have code such as class CString { int GetLength(); }; bool smaller(CString

  • 0

If I have code such as

class CString { int GetLength(); };

bool smaller(CString s1, std::string s2) {
    return s2.size() > s1.GetLength();
}

What is the best thing for me to do?

  • Change s1.GetLength() to (size_t)c.GetLength()?
    This would get help get rid of a compiler warning regarding "signed-unsigned mismatch", and communicate my intention to cast, and is by far the easiest route. But it’s probably frowned upon. 🙁

  • Change s1.GetLength() to static_cast<size_t>(c.GetLength())?
    This would get help get rid of the warning, with "The Correct" kind of cast.

  • Change s1.GetLength() to static_cast<std::string::size_type>(c.GetLength())?
    It’s extremely verbose… is there a practical benefit to this abstraction, or should I break it?

  • Leave it as is?
    This would help make the compiler do overflow-checking using the /RTCc switch (my main concern here), at the expense of a warning.

  • Do something else?
    Should I make my own casting function? Use a macro? Should I check at run-time as well as compile-time? Any other ideas?

Edit:

It seems like the example is being taken a little too literally…

I obviously didn’t mean to talk just about CString::GetLength(). That particular method is certainly not a huge worry of mine. 🙂 What I am worried about is the more general case, of when I’m getting an integer that’s never supposed to be negative, but which could theoretically be, due to bugs.

Heck, I might be writing a method that does this, in order to override another piece of code — so I can’t change the signature. And my code could certainly have bugs, even though I wouldn’t expect it.

In such a case, what should I do?

  • 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-01T08:14:06+00:00Added an answer on June 1, 2026 at 8:14 am

    Can you change GetLength()? Fundamentally, the issue is that length is never negative, and an unsigned type reflects that the best. Length shouldn’t be measured with an int.

    But other than that, all three of your solutions are identical. std::string::size_type is always std::size_t, and while I would use a static_cast, the C-style cast performs the same cast, in this case. Because you know that the returned length is never negative (ensure this, by the way; you never know what weird things people might do), you’re completely safe simply casting the type:

    return s2.size() > static_cast<std::size_t>(s1.GetLength());
    

    If CString::GetLength can be negative, for some reason, then it’s up to you to decide how to make that conversion from negative to positive. Truncate? Magnitude (absolute value)? Whatever you need.


    If you’re worried about bugs, either do an explicit check and throw an exception (depending on your domain, this may be too costly), or use assert. Generally, though, you should trust the documentation.

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

Sidebar

Related Questions

I have code like this: class MapIndex { private: typedef std::map<std::string, MapIndex*> Container; Container
I have such code: function allValid() { $('input').each(function(index) { if(something) { return false; }
I have a such code for Lithuanian declension: <?php $word = namas; $string =
I have such code, with downloaded from i-net class for db. I'm interested, how
I learning ruby singletons and have misunderstanding with such code: class MyClass def self.class_singleton_mymethod
I'm experementing with Jackson serialization/deserialization. For instance, I have such class: class Base{ String
I have, for example, such class: class Base { public: void SomeFunc() { std::cout
For reference, the code is for the motorola 68008. Say I have code such
I was wondering, if I have some code such as: $result = $db->query($sql); //
I have such code: <h:inputText id=input value=#{bean.input}> <f:convertNumber /> <rich:ajaxValidator event=onblur /> </h:inputText> 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.