2

I would like to implement a table that will consist of some names of data types so that I will be able use them in loop to cout my function. How to do it? I have already tried with the table that is in my code but it doesn't work. Probably it's something with pointers.

#include <iostream> 
#include <limits> 
#include <iomanip> 

using namespace std; 

template<typename T> 

void sizeF(string opis) { 
    int x = sizeof(T);
    if (x==1)
        cout << "Rozmiar typu "+opis+" to " << x << " bajt.\n";
    else if (x==2 || x==4)
        cout << "Rozmiar typu "+opis+" to " << x << " bajty.\n";
    else
        cout << "Rozmiar typu "+opis+" to " << x << " bajtów.\n";
} 

template<typename T> 
void maxMinF(string opis)
{
    cout << opis << ": minimalna wartosc: " << numeric_limits<T>::min() << ", maksymalna wartosc: " << numeric_limits<T>::max() << endl;
}

int main() 
{ 
    char tab[1][15] = {"short int"};
    sizeF<tab[0]>(tab[0]); 
    sizeF<int>("int");
    sizeF<unsigned long long>("unsigned long long");
    sizeF<bool>("bool");
    sizeF<char>("char");
    sizeF<double>("double");
    sizeF<long double>("long double");
    sizeF<long long>("long long");
    sizeF<short>("short");
    sizeF<unsigned short>("usigned short");

    cout << endl;

    maxMinF<short int>("short int"); 
    maxMinF<int>("int");
    maxMinF<unsigned long long>("unsigned long long");
    maxMinF<bool>("bool");
    maxMinF<char>("char");
    maxMinF<double>("double");
    maxMinF<long double>("long double");
    maxMinF<long long>("long long");
    maxMinF<short>("short");
    maxMinF<unsigned short>("usigned short");
    return 0; 
}
Enlico
  • 23,259
  • 6
  • 48
  • 102
GoldenRC
  • 91
  • 2
  • 8
  • You can use [`std::type_info`](https://en.cppreference.com/w/cpp/types/type_info) to obtain the typenames. To get them unmangled, you need to use a special function from your compiler's internals (e.g. `abi::__cxa_demangle` for GCC). – πάντα ῥεῖ Oct 15 '20 at 22:03
  • In c++ use std::vector>. If std::variant is not available, You may either use boost::variant or use union : ``` struct Type{ union { int i; unsigned long long i; //other types } enum class UsedType { IntType, ... } std::string data; }; and then... std::vector types; ``` You may now distinguish between types by UsedType enumaration. – Grzegorz Głowacki Oct 16 '20 at 08:52
  • in `cout << "Rozmiar typu "+opis+" to " << x` you're creating 2 temporary strings unnecessarily. Pass them directly to cout instead to avoid those temp strings: `cout << Rozmiar typu " << opis << " to " << x` – phuclv Oct 16 '20 at 09:12
  • @GrzegorzGłowacki could you tell me something more about IntType and next how to use std::vector types in my funciton? – GoldenRC Oct 18 '20 at 12:14

1 Answers1

1

I think you are taking a terrible route in doing what you're doing.

You should probably refer to Boost.TypeIndex library instead. Look at this:

#include <iostream>
#include <boost/type_index.hpp>

class Widget {
};

int main() {
    Widget w1;
    Widget& w2 = w1;
    Widget const w3;
    Widget const& w4 = w1;
    std::cout << boost::typeindex::type_id_with_cvr<decltype(w1)>() << '\n';
    std::cout << boost::typeindex::type_id_with_cvr<decltype(w2)>() << '\n';
    std::cout << boost::typeindex::type_id_with_cvr<decltype(w3)>() << '\n';
    std::cout << boost::typeindex::type_id_with_cvr<decltype(w4)>() << '\n';
}

which outputs exactly what you'd expect:

Widget                                                                                                                       
Widget&                                                                                                                      
Widget const                                                                                                                 
Widget const&

Here's the demo.

Enlico
  • 23,259
  • 6
  • 48
  • 102
  • Thank you for your answer. I had some problems with installing Boosted library. Finally I don't know how to add it to my code in VS Code. Is there an easier way to do it? – GoldenRC Oct 18 '20 at 11:42
  • 1
    @GoldenRC, frankly, I don't know, especially if you are on Windows/Mac. I'm on ArchLinux, where installing ordinary stuff or bleeding edge stuff is just easy. Maybe you can search for an existing question. There's one for sure, I guess. And maybe you can accept mine, no? I've added a demo too. – Enlico Oct 18 '20 at 11:51
  • 1
    [This could be useful for your installing question](https://stackoverflow.com/questions/56661801/how-can-i-install-boost-in-windows-10-with-vs-2019-preview). – Enlico Oct 18 '20 at 11:52