0
#include <iostream>

typedef uint32_t type1;

struct type2
{
    type2(uint32_t i) : i(i) {}
    inline operator uint32_t() { return i; }
private:
    uint32_t i;
};

void x(uint32_t num) {
    std::cout << "uint32_t: ";
    std::cout << (num << 30) << std::endl;
}

// redefinition of x
// void x(type1 num) {
//     std::cout << "type1: ";
//     std::cout << (num << 30) << std::endl;
// }

void x(type2 num) {
    std::cout << "type2: ";
    std::cout << (num << 30) << std::endl;
}

int main()
{
    uint32_t num = 15;
    type1 num1 = 15;
    type2 num2 = 15;

    x(num);
    x(num1);
    x(num2);
}

Here, I want to create another type that is equivalent to uint32_t. if I use typedef, I'll just create an alias, and I won't be able to overload functions using it.

creating a struct and adding a cast operator is one way of doing it, but it's a bit of a boilerplate.

I'm not even sure whether this is optimal or not. Should I make the cast operator inline as in this code, so that no function is being called for casting? Or should I assume that the compiler is smart enough to optimize this and treat it just as a uint32_t?

Is there a better way of doing this?


Update: I meant a user-defined type that behaves like a primitive data type of choice. This way, I get the speed and the already defined operators and functions for that type for free.

StackExchange123
  • 1,871
  • 9
  • 24
  • 2
    You can not create a new primitive type. You can introduce an alias for already existent primitive types. – Vlad from Moscow Sep 24 '21 at 22:44
  • 1
    I don't know of any better way to make a type equivalent to a primitive type, but distinct. Note also that you'll have to write all the arithmetic for your struct too, because e.g. `type2(1)+type2(2)` won't benefit from the cast operator. – Ruslan Sep 24 '21 at 22:48
  • For `type2`, you should overload `operator=()`, a.k.a. the assignment operator. Also, search the internet for "C++ rule of 0, 3, 5". – Thomas Matthews Sep 24 '21 at 22:48
  • If you have the source code to the compiler, the *better method* is to modify the compiler when adding a primitive type. However, your compiler will not be C++ compliant anymore. – Thomas Matthews Sep 24 '21 at 22:49
  • Why do you need a new primitive type? – Thomas Matthews Sep 24 '21 at 22:49
  • 1
    *What is the best way of creating a new primitive data type?* By either creating your own language that supports that, or using a language which already supports that (e.q., [**D**](https://dlang.org/)). C++ does not support it. – Eljay Sep 24 '21 at 22:51
  • You could use an `enum class : uint32_t`, but you would have to define all of the operators for it as well – NathanOliver Sep 24 '21 at 22:52
  • 1
    If you want to make a *user defined type* that has many of the behaviors of a primitive data type, take a look at Abseil's [int128.h](https://chromium.googlesource.com/external/github.com/abseil/abseil-cpp/+/HEAD/absl/numeric/int128.h) for inspiration. Takes a lot of boilerplate to emulate a primitive type. – Eljay Sep 24 '21 at 22:54
  • 1
    *Is there a better way of doing this?* -- What is the actual problem you're trying to solve, where you need to implement your own user-defined primitive type? – PaulMcKenzie Sep 24 '21 at 23:32
  • @PaulMcKenzie I found it in Stockfish's source code. they define `Score` as an enum instead of `uint32_t` to be able to overload. I was wondering if there is a better way of doing it. – StackExchange123 Sep 24 '21 at 23:36

0 Answers0