2

In c++, a pointer type (void*/int*/char*) can be initialized with a value of 0 without type casting, but this doesn't work for any other value. Is it possible to instill the same behavior into a defined class? An =0 constructor, or an ==0 comparator? For clarification, I'm attempting to add syntax such that my n-dimensional vector class can be assigned to a zero vector with vec<n> dir = 0

Edit: using void* as the type of a constructor argument does in fact work, is this the best way to achieve this though?

TechNeko
  • 35
  • 5
  • Please explain how this is a duplicate? Reading through the recommended thread I'm just as confused as I was coming into this, and it didn't establish a method to achieve my goal. What's with questions on this website being so quickly shut down??? – TechNeko May 23 '19 at 08:01
  • This looks a lot like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Why would you want to do that? – molbdnilo May 23 '19 at 08:02
  • @molbdnilo "_You can't add implicit conversions from literal 0_" ? probably you wanted to say "can" rather than "can't" – bruno May 23 '19 at 08:10
  • 1
    Please note that I didnt "shut down" your quesiton. I flagged it as dupe, because I think 2/3 of your question is answered in the dupe. However, if you dont agree, I happily reopen it – 463035818_is_not_an_ai May 23 '19 at 08:10
  • @molbdnilo I'd like the ability to assign my n-dimension vector class such as `vec3 dir = 0` – TechNeko May 23 '19 at 08:11

2 Answers2

5

There is an implicit conversion from literal 0 to nullptr, and the type of nullptr is nullptr_t.

Thus, the following works the way you want:

// Needed for nullptr_t
#include <cstddef> 

struct A
{
    A(std::nullptr_t) {}
};

int main()
{
    A a = 0;
}

Of course, this also means that A a = nullptr is valid, but perhaps you can live with that...

It's possible that you can accomplish this without the indirection through nullptr_t with some modern constexpr and template shenanigans.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • bonus points for reminding me of both constexpr and - as you put it - template shenanigans. I looked into it briefly after you mentioned it but for now I'll go for the null_ptr option because intellisense seems to be a big fan. – TechNeko May 24 '19 at 21:10
3

You can't make a constructor that accepts only the literal value 0. There are a couple of things that get close.

#include <cstddef>

class Foo
{
    Foo(); // Construct from nothing at all
    Foo(std::nullptr_t) // Construct from the null pointer value
};

Which are used respectively

Foo foo_default;
Foo foo_null = nullptr;

Because 0 is also a null pointer value, you can use the std::nullptr_t overload with that.

Foo foo_zero = 0; // calls Foo(std::nullptr_t)
Caleth
  • 52,200
  • 2
  • 44
  • 75
  • @L.F. I tried this, and didn't seem to be able to work - my class was either accepting nullptr_t as well or refusing 0 because it was ambiguous as to which constructor to use. If you can get a piece of short example code to work it would be much appreciated – TechNeko May 23 '19 at 08:30
  • @bruno That comment was actually in response to a follow up suggestion that L.F. commented which didn't seem to work. The original answer does in fact work, and was my original pick for the answer, I only swapped it because I thought the other answer was a little clearer as to what worked and why :) – TechNeko May 24 '19 at 21:08