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

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    129

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

    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:

    Code:
    #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?

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,466
    typeof is a gcc extension. typeid is part of C++, but you must include <type_info>. decltype doesn't need <type_info>. It can be used like this (maybe there's an easier way, but this works).
    Code:
    #include <iostream>
     
    template<class T, class U> struct is_same {
        static constexpr bool value = false;
    };
     
    template<class T> struct is_same<T, T> {
        static constexpr bool value = true;
    };
     
    int main()
    {
        using std::cout;
        double a;
        int b;
     
        if (is_same<decltype(a), int   >::value) cout << "a is int\n";
        if (is_same<decltype(a), double>::value) cout << "a is double\n";
        if (is_same<decltype(b), int   >::value) cout << "b is int\n";
        if (is_same<decltype(b), double>::value) cout << "b is double\n";
    }
    The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts. - Bertrand Russell

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,436
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2021
    Posts
    129
    Quote Originally Posted by john.c View Post
    typeof is a gcc extension. typeid is part of C++, but you must include <type_info>. decltype doesn't need <type_info>. It can be used like this (maybe there's an easier way, but this works).
    Code:
    #include <iostream>
     
    template<class T, class U> struct is_same {
        static constexpr bool value = false;
    };
     
    template<class T> struct is_same<T, T> {
        static constexpr bool value = true;
    };
     
    int main()
    {
        using std::cout;
        double a;
        int b;
     
        if (is_same<decltype(a), int   >::value) cout << "a is int\n";
        if (is_same<decltype(a), double>::value) cout << "a is double\n";
        if (is_same<decltype(b), int   >::value) cout << "b is int\n";
        if (is_same<decltype(b), double>::value) cout << "b is double\n";
    }
    Thanks a lot!!!! It works as expected!!! Now I'll probably use C++ over D

  5. #5
    Registered User
    Join Date
    Oct 2021
    Posts
    129
    Quote Originally Posted by Salem View Post
    Yeah, that's from me. I decided to post in both these forums to get more chances to get answers. But it seems nobody other than you answer here while we already have one more answer here which also solves my problem. I think I'll give the other forum a try for my next question in it will be decided if I'm going to posting only here after that.

  6. #6
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    In case someone is interested. The <type_traits> header has a std::is_same type already.
    std::is_same - cppreference.com

  7. #7
    Registered User
    Join Date
    Oct 2021
    Posts
    129
    Quote Originally Posted by thmm View Post
    In case someone is interested. The <type_traits> header has a std::is_same type already.
    std::is_same - cppreference.com
    Thanks! Do you know how is this implemented? I wonder if this is going to be faster to compile and lighter on runtime than the answer given by john.c.

  8. #8
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    I guess each compiler might implement it differently. If it is faster only a benchmark can tell.
    To test it easily and quickly, have a look at Quick C++ Benchmarks

  9. #9
    Registered User
    Join Date
    Oct 2021
    Posts
    129
    Quote Originally Posted by thmm View Post
    I guess each compiler might implement it differently. If it is faster only a benchmark can tell.
    To test it easily and quickly, have a look at Quick C++ Benchmarks
    Thanks! I will!
    Last edited by rempas; 12-14-2021 at 11:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to declare an external variable of type "enum"?
    By Adam Rinkleff in forum C Programming
    Replies: 28
    Last Post: 06-23-2011, 09:52 PM
  2. Replies: 3
    Last Post: 05-01-2010, 02:26 AM
  3. Why I can not "extern" a variable from header file?
    By meili100 in forum C++ Programming
    Replies: 22
    Last Post: 06-23-2008, 03:58 AM
  4. problem about handle "double" variable type? or?
    By Mathsniper in forum C Programming
    Replies: 4
    Last Post: 12-31-2006, 10:11 PM
  5. "Deciding" in runtime the type of a variable
    By mikahell in forum C++ Programming
    Replies: 28
    Last Post: 07-22-2006, 09:51 AM

Tags for this Thread