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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:24:14+00:00 2026-06-05T12:24:14+00:00

I am implementing a SI unit type system. To ensure units don’t leak in

  • 0

I am implementing a SI unit type system. To ensure units don’t leak in and out, I don’t want implicit conversion of any values to a unit, and vice-versa. However, it would be really convenient to be able convert 0 to my unit system, a bit like you can do with pointers, where 0 implicitly converts to a null pointer, but no other value does.

So my question is: can I replicate this kind of implicit conversion of 0 to null pointer? Or can I use it to my advantage to achieve the same thing?

For example what would you think of such a constructor?

constexpr unit_t(void*) : _value(0) { }

Note: the unit itself is in the type, not in the value.

Edit: Why casting from 0 to a unit?

The reason I want this, is to write algorithms that don’t know about units. For example, if you have a matrix of lengths and you want to invert it, you first compute its determinant, and if it’s not 0, then return the inverse (otherwise, this is an error).

So it would convenient to treat the 0 literal as a generic unit. And this is exactly what happens for pointers. You can type:

void* c = 0;

But not:

void f(int a) { void *c = a; }
  • 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-05T12:24:15+00:00Added an answer on June 5, 2026 at 12:24 pm

    The classical solution is to a pointer to some private type, that the user can’t name. That way, the only way he can get something that will match is through the implicit conversion from 0. (Using void* means that practically anything which can convert to a pointer can be passed.)

    Note that this solution only works for integral constant 0. If the user wants to use 0.0 (because he thinks that double values make more sense), he can’t.

    You might also consider using a default constructor, so the client can simply write SomeUnit() when he wants the default initialization.

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

Sidebar

Related Questions

I'm implementing unit testing with PHPUnit on an existing codebase. I'm fairly new to
I have a Visual Studio 2008 C++ project where I am implementing unit tests
We've seen some very useful methods of implementing EF4 Repository and Unit of Work
I'm implementing unit tests for a family of functions that all share a number
I am implementing Unit of Work. This is part of my interface: public interface
I have been having quite a bit of trouble implementing unit testing on the
I have created an Android application that includes some unit conversion tools built into
I am new to repositories. I just read about implementing predicates and a Unit
I am going to start implementing some unit tests for a codebase that is
I have been using http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application as guidance to help me create a repository.I have

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.