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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:53:03+00:00 2026-05-27T16:53:03+00:00

In C++, I want to have a class whose constructors are the following: class

  • 0

In C++, I want to have a class whose constructors are the following:

class A {
  explicit A(A* other) { ... }
  explicit A(intptr_t other) { ... }
};

The problem with this is if the user initialize with

A a(0);

Then, on a 64 bits system, the compiler will complain that it doesn’t know if 0 should be converted to A* or to intptr_t, which is fair enough. As I want this simple notation to work, I added the following constructor:

explicit A(int a) { assert(a==0); ... }

The assertion is because this is the only integer this makes sense for. Now, the problem arises with a 32 bits system, in which intptr_t is actually … int ! So now, the system complains there are two constructors taking the same parameter type (which, again, is fair enough).

So my question is: is there a way with the preprocessor to detect that intptr_t is actually int and, in that case, not compile the constructor with int. Or, is there another way to make the A a(0) notation valid without adding the constructor with int, but without removing either of the two first constructor (and not making them implicit either).

  • 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-27T16:53:04+00:00Added an answer on May 27, 2026 at 4:53 pm

    Something like

    #if INTPTR_MAX == INT_MAX
    

    might do the trick, but it will still result in true where long and
    int are the same size, and ptrint_t is a typedef for long.
    Another possibility (but don’t know whether you can use it or not)
    would be to use uintptr_t, rather than intptr_t.

    Beyond these: the preprocessor doesn’t know about types, so the problem
    can’t be solved there. You’ll have to use some sort of meta-programming
    trick: you make the int constructor a template, using
    boost::enable_if to activate it only if the argument has type int.
    If ptrint_t is int, then the activated function will never be
    used, because it will never be a better match than the non-template
    function with the same signature. If ptrint_t isn’t int, then the
    template instantiation will be a better match when the argument has type
    int. (Note that I’ve never tried this myself: it sounds to me like it
    should be possible, but I’m not that familiar with boost::enable_if to
    be sure.)

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

Sidebar

Related Questions

I have: class Car {..} class Other{ List<T> GetAll(){..} } I want to do:
Want to verify that my understanding of how this works. Have a C++ Class
I have a class whose new method has been made private because I want
I have a class and I want to have some bit masks with values
I want to have a class which implements an interface, which specifies the specific
I want to have a class property that allow for an expression to take
I want to have a base class which dictates the alignment of the objects
I want to have a template class that looks something like what I have
I have a class that inherits QMainWindow and I just want it to have
I have class A: public B { ...} vector<A*> v; I want to do

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.