How to get the type of a variable without the "typeinfo" header?

closed account (EUoNwbRD)
I don't want to link the C++ (not even C's libc) and I want to learn how (and if) I can get the type of a variable for a comparison. For example I want to do something like the following:

1
2
3
4
5
6
7
8
9
#include <stdio.h>

template <typename T>
void print_type(T num) {
  if constexpr(typeof(num) == typeof(double)) {
    printf("It is a double!");
  } else if constexpr(typeof(num) == typeof(double)) {
    printf("It is an integer!"); }
}


Is there any way to do that without using the C++ "typeinfo" header?
Last edited on
Making the same posts in multiple forums is not going to get you more answers faster. If known it will piss off the regulars and be seen as spamming.
> I don't want to link the C++ (not even C's libc)

Even in a freestanding implementation of C++, there would be a scaled down version of the standard library; you would have it whether you want it or not.
https://eel.is/c++draft/compliance#tab:headers.cpp.fs

<typeinfo> is one of the specified headers; other than for <cstdlib> and <atomic>,
"The other headers listed in this table shall meet the same requirements as for a hosted implementation."
https://eel.is/c++draft/compliance#3


> I want to learn how (and if) I can get the type of a variable for a comparison.
> Is there any way to do that without using the C++ "typeinfo" header?

Overload the function for each type of interest.
Maybe is_same<...>. See:

http://www.cplusplus.com/reference/type_traits/is_same/?kw=is_same

It requires <type_traits> but most likely no lib.
the 'any' type knows its type.
variant has a clunky way as well, you can just ask it 'is it an integer, is it a char, is it a string ...' for all the types you care about until you score.
I don't know what these pull in at link time.
I mean, you can also go all C on it and make an enum for all your types and use a void*, tie them together in a struct and its done.
Last edited on
closed account (EUoNwbRD)
Making the same posts in multiple forums is not going to get you more answers faster. If known it will piss off the regulars and be seen as spamming.


Understandable, I will carefully choose the forum I want to stay in after this question.

Even in a freestanding implementation of C++, there would be a scaled down version of the standard library; you would have it whether you want it or not.


Sorry maybe my English are not great and I didn't got what you mean here. From what I understand is that you are saying that no matter what I will not some implementation of the library because I don't want to do everything in Assembly right? In this case then I don't we I MUST do that, I can be crazy in the end and want to reinvent the wheel... Hoever, I want to create my own system library as a replacement to libc and libc++ (or libstd++ idk how it is called) and keep using that in my projects. So this is the main point of not using any system library.

Maybe is_same<...>. See:

http://www.cplusplus.com/reference/type_traits/is_same/?kw=is_same

It requires <type_traits> but most likely no lib.


Thanks! Yeah you are right, it requires no lib and works as expected! However, do you know how is it implemented?

Another answer I got is this one: https://cboard.cprogramming.com/cplusplus-programming/180735-how-get-type-variable-without-typeinfo-header.html#post1303830

And I'm wondering which one of these two will be more efficient to compile faster and have better runtime performance (maybe, I don't care a lot about this one tbh) but I care mostly about compilation times.

the 'any' type knows its type.
variant has a clunky way as well, you can just ask it 'is it an integer, is it a char, is it a string ...' for all the types you care about until you score.


The thing is that I want a general "global" way to have the user of my library to check if two types match (one one if classes inherit from one another) so this will not work for me

I mean, you can also go all C on it and make an enum for all your types and use a void*, tie them together in a struct and its done.


Well, TCC (tiny c compiler in case you don't know) made my trying as much as possible to stretch C and get the most possible out of it but I just can't. I want to use a C++ compiler but I don't want to use libc and libc++ (or whatever it's called) because I will create a system library myself.
If I remember correctly there is a user here called @freddie1 who once wrote his own version of the libc library. Maybe you can contact him.
closed account (EUoNwbRD)
If I remember correctly there is a user here called @freddie1 who once wrote his own version of the libc library. Maybe you can contact him.


Whaaaaaaaaa? He wrote his own libc (using assembly) or libc++? (using the libc functions)? If you mean the first then I'm going to contact him to tell him that he is a damn legend!!!!
It's a long time ago so I don't remember all. I think he wrote it in C or C++, probably not in assembler.
> do you know how is it implemented?

Possible implementation:
https://en.cppreference.com/w/cpp/types/is_same#Possible_implementation
closed account (EUoNwbRD)
> t's a long time ago so I don't remember all. I think he wrote it in C or C++, probably not in assembler.

I mean inline assembly. If there are no built-in functions (like how "unistd.h" wraps system calls for you), it is necessary to write inline assembly for the system calls yourself (and I suppose there are other things that you can do but I don't know). This is what I want to do.
closed account (EUoNwbRD)
Possible implementation:
https://en.cppreference.com/w/cpp/types/is_same#Possible_implementation


Thanks a lot!
However, do you know how is it implemented?
s_same<...> and if constexpr are resolved at compile time. So there is no implementation in the code sense.

s_same<...> will be resolved to true or false.
if constexpr allows or disallows the following code to take place.

So they're more a compiler instruction than anything else.
George P wrote:
Making the same posts in multiple forums is not going to get you more answers faster. If known it will piss off the regulars and be seen as spamming.
Honestly, I don't see why it shouldn't be allowed to post on different sites/forums...
If you do post to multiple sites, it's good etiquette to indicate those other sites.
Why is cross-posting bad?

1. The OP doesn't consider where would be the best place to post, so they broadcast:
http://www.catb.org/~esr/faqs/smart-questions.html#forum

2. It feigns urgency, by hoping more people will read it sooner rather than later:
http://www.catb.org/~esr/faqs/smart-questions.html#urgent

Trust me, when you've seen your answer posted by someone else on another forum in reply to the same question a few times, the novelty soon wears off.

I REALLY hate having my time wasted by selfish cross-posting snowflakes thinking theirs is the single most important post on the web right now.

And it's dead time - time which CANNOT be used to help someone else instead, because it was wasted repeating what has already been said.

So yeah, whenever I see cross-posts, I put the cross-links in.

And no, this isn't for the benefit of the OP, no matter how much they whine about being called out.
It's for the benefit of the regular helpers, who can quickly check whether what they were thinking of saying has already been said elsewhere.

Of course, the cross-posting OP might soon get tarred with the persistent cross-poster brush and routinely ignored on all forums - which is a bonus.
Honestly, I don't see why it shouldn't be allowed to post on different sites/forums...


It is allowed. Its discouraged without links, though, for reasons listed.
Most of the questions are so basic that it takes longer to look up the cross ref than to answer.
Topic archived. No new replies allowed.