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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:59:57+00:00 2026-06-07T03:59:57+00:00

I want to define a new type in C++ which is just some primitive

  • 0

I want to define a new type in C++ which is just some primitive type (in my example an int, could be any type). I call the type NodeId in this example.

I could just use typedef int NodeId. I want a default value for NodeIds, so I would use #define NULL_NODE_ID -1.

Now, I think it would be nicer to define a class instead of typedef to allow a function isValid() and a default constructor constructing a null NodeId:

class NodeId
{
    int value;
public:
    inline NodeId() : value(-1) {}
    inline NodeId(int value) : value(value) {}
    inline operator int() {return value;}
    inline bool isValid() {return value != -1;}
    //...
};

Are there any performance disadvantages which result in using the second method?

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

    Actually, there are two reasons this could conceivably be slower.

    First, there’s no way to create an uninitialized NodeId. Normally, that’s a good thing. But imagine you have code like this:

    NodeId nodeid;
    foo.initializeNodeId(&nodeid);
    

    You’ll be doing an extra assignment which isn’t actually necessary.

    You can fix that by adding a special constructor. It’s probably much better to create a Foo::createNodeId() so you don’t need Foo::initializeNodeId(&NodeId), but if you don’t control the definition of Foo, that may not be possible.

    Second, a NodeId is not a compile-time constant expression. As dasblinkenlight suggests, this is far more likely to cause problems with code not being legal than to cause performance problems, but both are possible. (Why? Because you may be forcing the compiler to insert code to do some calculation at runtime that could have been done at compile time, if you were using an int. Not that this is likely to be an issue for a class called NodeId…)

    Fortunately, if you’re using C++11, you can fix that with constexpr. And if you want your code to also be legal C++03, you can deal with that with a macro.

    Also, as pointed out by dasblinkenlight, you’re missing const in two methods.

    Finally, there’s no reason to write “inline” on methods that are defined inside the class definition; they’re already inherently inline.

    Putting it all together:

    #if __cplusplus > 201000L
    #define CONSTEXPR_ constexpr
    #else
    #define CONSTEXPR_
    #endif
    
    class NodeId
    {
        int value;
    public:
        struct Uninitialized {};
        CONSTEXPR_ NodeId() : value(-1) {}
        CONSTEXPR_ NodeId(Uninitialized) {}      
        CONSTEXPR_ NodeId(int value) : value(value) {}
        CONSTEXPR_ operator int() const {return value;}
        CONSTEXPR_ bool isValid() const {return value != -1;}
        //...
    };
    

    Now you can do this, to avoid the cost of an extra -1 assignment.

    NodeId nodeId(NodeId::Uninitialized());
    foo.initializeNodeId(&nodeid);
    

    And this, to legally use a NodeId as a non-type template parameter:

    myClassTemplate<NodeId(3)> c;
    

    Or, this, to be sure the compiler can legally just initialize x to 4:

    int x = 3;
    x += NodeId(1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to define a new abstract data type which is either a general
At first sorry my bad English. I want define routing for this url: For
I use emacs in ubuntu 12.04. i want define some background or foreground in
I want to define some properties on a class using the [Indexable()] attribute in
I want to define what seems to require an infinite type. Required : a
I'd like to define a class which is derived from System.Data.DataTable. This class has
I want to create a new element with html5 data attributes defined, however they
I want to define the data.store that store companyList, because I want to reuse
I want to define a generic function for printing contents of std::map like types.
I want to define a class that is able to populate itself reading from

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.