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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:19:12+00:00 2026-06-03T00:19:12+00:00

EDIT: Following Mike Seymour’s comment, I replaced operator std::string () const; with operator char

  • 0

EDIT: Following Mike Seymour’s comment, I replaced operator std::string () const; with operator char * () const; and changed the implementation accordingly. This allows implicit casting, but, for some reason, the unsigned long int operator has precedence over the char * operator, which just does not feel right… Also, I don’t want to expose nasty C stuff like char * outside the class, when I have std::string. I have a hunch that my CustomizedInt class needs to inherit from some stuff in order to support the feature that I desire. Could anybody please elaborate Mike’s comment regarding std::basic_string? I’m not sure I understood it properly.


I have this piece of code:

#include <string>
#include <sstream>
#include <iostream>

class CustomizedInt
{
private:
    int data;
public:
    CustomizedInt() : data(123)
    {
    }
    operator unsigned long int () const;
    operator std::string () const;
};

CustomizedInt::operator unsigned long int () const
{
    std::cout << "Called operator unsigned long int; ";
    unsigned long int output;
    output = (unsigned long int)data;
    return output;
}

CustomizedInt::operator std::string () const
{
    std::cout << "Called operator std::string; ";
    std::stringstream ss;
    ss << this->data;
    return ss.str();
}

int main()
{
    CustomizedInt x;
    std::cout << x << std::endl;
    return 0;
}

Which prints “Called operator unsigned long int; 123”. My questions are these:

  1. After I remove the operator unsigned long int, why do I need to cast x to std::string explicitly? Why does it not call the implicit cast operator (std::string) directly?
  2. Is there any documentation that explains which implicit casts are allowed and which is their order of precedence? It seems that if I add an operator unsigned int to this class together with the operator unsigned long int, I receive a compiler error about ambiguity for the << operator…
  3. Also, I know that defining such an operator may be poor practice, but I am not sure I fully understand the associated caveats. Could somebody please outline them? Would it be better practice to just define public methods ToUnsignedLongInt and ToString?
  • 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-03T00:19:14+00:00Added an answer on June 3, 2026 at 12:19 am

    After I remove the operator unsigned long int, why do I need to cast x to std::string explicitly? Why does it not call the implicit cast operator (std::string) directly?

    The version of << for strings is a template, parametrised by the parameters of the std::basic_string template (std::string itself being a specialisation of that template). It can only be chosen by argument-dependent lookup, and that only works if the argument is actually a specialisation of std::basic_string, not something convertible to that.

    Is there any documentation that explains which implicit casts are allowed and which is their order of precedence?

    The rules are quite complex, and you’d need to read the C++ standard for the full story. Simple rules of thumb are that implicit conversions can’t contain more than one user-defined conversion and (as you’ve found out) the result of an implicit conversion can’t be used to choose a template specialisation by argument-dependent lookup.

    I am not sure I fully understand the associated caveats. Could somebody please outline them?

    I don’t fully understand them either; the interactions between implicit conversions, name lookup and template specialisation (and probably other factors that I can’t think of right now) are rather complex, and most people don’t have the inclination to learn them all. There are quite a few instances where implicit conversion won’t happen, and others where it might happen when you don’t expect it; personally, I find it easier just to avoid implicit conversions most of the time.

    Would it be better practice to just define public methods ToUnsignedLongInt and ToString?

    That’s probably a good idea, to avoid unwanted conversions. You can fix your problem by leaving them and use them explicitly when necessary:

    std::cout << std::string(x) << std::endl;
    

    In C++11, you can declare them explicit, so that they can only be used in this manner. In my opinion, that would be the best option if you can; otherwise, I would use explicit conversion functions as you suggest.

    By the way, the return type of main() must be int, not void.

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

Sidebar

Related Questions

I am using the following code --- EDIT @annakata : Adding the complete code
I register the following function //EDIT: updated $("#id").on("change", function() { alert('xxx'); }); In my
Im trying to hammer togehter a WYSIWYG-edit in c# following some examples from here
I'm looking to analyze and compare the following `signals': (Edit: better renderings here: oscillations
EDIT How to simplify the following code: if(x(a) > x(b)) s = b; e
Edit: Warning - I now realize that the following technique is generally regarded as
EDIT: I am basically running into the following documented issue . I am using
[Edit] I've summarized the answer to the following below, the error lies in the
EDIT: OK, I believe the following solutions are valid: Use the jQuery AOP plugin.
Take for instance the following code for Edit action in my controller: // POST:

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.