0

I have a class named NumBase, and I want to initialize the objects of that class with a special customization, like shown below:

NumBase<8> Num1 = 2312347;
NumBase<10> Num2 = 23123479;
NumBase<16> Num3 = 23F13A47;
NumBase<12> Num4 = 2312A47;

Is there any way to initialize exact like this way?

Or

NumBase(8) Num1 = 2312347;
NumBase(10) Num2 = 23123479;
NumBase(16) Num3 = 23F13A47;
NumBase(12) Num4 = 2312A47;

also

NumBase<8> Num1(2312347);
NumBase<10> Num2(23123479);
NumBase<16> Num3(23F13A47);
NumBase<12> Num4(2312A47);

but not like this:

NumBase<16> Num3("23F13A47");

or

NumBase<16> Num3 = "23F13A47" ;

In other words, I don't want to use strings.

If it is possible, how can I do it?

Wolf
  • 9,679
  • 7
  • 62
  • 108
  • 1
    If you can introduce proper syntax for a hexadecimal literal: `0x23F13A47`, then there won't be a problem. – rawrex Jul 07 '21 at 09:31
  • What's wrong about `02312347` (oct), `23123479` (dec), and `0x23F13A47` (hex)? – Wolf Jul 07 '21 at 09:31
  • I don't think it's possible. `23F13A47` is not a single token, compiler sees it as `23` with custom literal `F13A47` – Yksisarvinen Jul 07 '21 at 09:32
  • there are decimal, hex and octal integer literals: https://en.cppreference.com/w/cpp/language/integer_literal – 463035818_is_not_an_ai Jul 07 '21 at 09:34
  • Please tell us ***why* you don't want to use strings** and specify limits: which bases do you need to be supported? `2..36`? Maybe also unary? – Wolf Jul 07 '21 at 09:42
  • not sure if [user defined literals](https://en.cppreference.com/w/cpp/language/user_literal) can help here, i never used them, but I believe it wont bring much advantage over using string literals – 463035818_is_not_an_ai Jul 07 '21 at 09:44
  • I reworded your question. Does it still meet your intent? – Wolf Jul 07 '21 at 11:23
  • Why so many C++ version tags? Do you need a solution that works in each of those versions? If not, just tag the highest version (C++20 in this case). – rustyx Jul 07 '21 at 11:30
  • @rustyx I think this is a general question, examples and terminology used (so far) show that there are no special expectations (or knowledge) to specific C++ standards. So I removed all but [tag:c++] – Wolf Jul 07 '21 at 14:18

1 Answers1

1

This is not possible via user-defined literals as available since C++11, because these literals are built by combining a traditional literals with a user defined prefix. The reason for that is that the C++ lexical analysis process cannot be altered via user-defined types.

The nearest approach to your goal using standard means is a combination of a string literal with a user-defined suffix, so something like this

unsigned operator "" _nb(const char* str);

or that

unsigned operator "" _nb(const char* str, std::size_t len);

The len gives you the length of the string literal. This keeps the of translation to you. You may provide specialized versions of the string-to ?-digit decoding for optimization.

See also User Defined Literals for a String versus for a Hex Value for details.

Wolf
  • 9,679
  • 7
  • 62
  • 108