Thread: Distinguishing types

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    7

    Distinguishing types

    Hi,

    I was wondering if someone could help me.
    I have a template function where based on the type passed in
    I have to possibly access a struct member or just print the value.
    Yet when I try to compile this it fails with tons of errors.

    Code:
    #include <typeinfo>
    #include <iostream>
    
    using namespace std;
    
    typedef struct {
        double a;
        double b;
    } A;
    
    template<class E> void check_el(E val) {
      if(typeid(E) == typeid(int)) {
        cout << val << endl;
      } else if(typeid(E) == typeid(A)){
        double t = val.a;
        cout << t << endl;
      } else {
        cout << "NOTHING" << endl;
      }
    }
    
    int main() {
        A a = {1, 2};
        check_el<A>(a);
        check_el<int>(5);
    
        return 0;
    }
    How would I go about fixing this?

    Thanks a lot for any help,
    Tony

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    My guess is that you can't use templates with typeid, I might be wrong though. Simple remark, however, you don't need to use the typedef trick for structures in C++. It's a problem that's been fixed from C to C++.

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Instead of trying to implement a function that determines the type of data passed in, you will have more success outputting the data as you normally would with cout.

    To do so you will merely need to provide an ostream << operator overloading function for each structure that handles its output.

    Then, whenever you do something like:
    Code:
    A val;
    val.a = 10;
    val.b = 15;
    cout << val << endl;
    It will automatically output the structure as you implemented it with the overloaded function.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Using templates with typeid is perfectly valid. The problem is that the ifs are runtime checks, not compile-time, so the entire function must compile properly with whatever type you put in. So even if T is int, the compiler expects the argument to have an 'a' member, and will throw an error if it doesn't.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    7
    Thanks a lot for the suggestions and pointers guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid types 'int[int]' for array subscript
    By kolistivra in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2010, 12:57 PM
  2. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  4. Types, Integral Types, Bytes?!?!?!
    By Kaidao in forum C++ Programming
    Replies: 3
    Last Post: 03-21-2006, 08:15 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM