Thread: Deducing type traits from a variable.

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    752

    Deducing type traits from a variable.

    The following fails...
    Code:
    int main (void) {
       std::vector<int> my_vector;
    
       // Some code
    
       // At this point, I dunno what my_vector is
       my_vector::const_iterator ci = my_vector.begin(); // Error
    
       // ...
    
       return 0;
    }
    as does
    Code:
    my_vector.const_iterator ci = my_vector.begin();
    Basically, I want to avoid looking up the type, and let the compiler handle that for me. Am I trying to do something here that C++ doesn't allow? It seems like this should be possible somehow.
    Callou collei we'll code the way
    Of prime numbers and pings!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    How can you not know what type my_vector is? You have to know.

    The next standard will use auto to make this easier, but for now unless I'm missing something you should know what type my_vector is and you have to use that. If it is just too ugly or verbose, consider a typedef for the type.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could use a template function.
    Code:
    template <typename T>
    void function( /* ... */) {
        std::vector<T> x;
        std::vector<T>::iterator i;
    }
    Or you could declare the type somewhere (with a typedef?) and use that.

    But these are just ways of getting around the issue, while still putting the type in.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by kryptkat View Post
    Er...no. Only if you want to compile as C++/CLI. Instead, you could use RTTI if you really wanted to go that route.

    At first I was kind of surprised there isn't a base non-templated iterator that every iterator derives from. Then I tried to find a way to use the thing, and I just couldn't (except for counting). You've got to know the type.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You can't access nested typenames through an object, you have to use a qualified typename. What you're really looking for is a function that returns a type instead of an object, but such a thing can't exist in C++.

    Since a variable must always be declared with some concrete type, you will never find a situation where you do not know the type of the variable. So you can always just refer to the concrete type, in this case, std::vector<int>::const_iterator. It kind of sucks, but that's the way it is.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Well, thanks guys, and I'm sorry to hear it can't happen. A few notes...

    • When I said "I don't know", I meant me as the coder. Figuring out a given variable's type becomes a non-trivial exercise once things get complicated enough, and typedefs can hurt as much as they help.
    • A second question, related. Is there a reason C++ does support this? Offhand, it seems like a good feature that's (relatively) trivial to implement. The fact that it can't be done makes me think there's some fundamental problem with it that I am missing.
    Callou collei we'll code the way
    Of prime numbers and pings!

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> When I said "I don't know", I meant me as the coder.
    I've been there, but in reality in every case you can find out, so that's just what you have to do.

    >> Is there a reason C++ does support this?
    C++ doesn't support this because it isn't necessary. You can always find out the type you need to use by looking at the code.

    But as I said, they are adding a feature in the next release that does what you want, mostly for the reasons you want it. It can be annoying to try to figure out the exact type needed there, and it should be relatively easy for the compiler to figure it out from the context. Search for the auto keyword and C++0x for more information.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by QuestionC View Post
    [list][*]When I said "I don't know", I meant me as the coder. Figuring out a given variable's type becomes a non-trivial exercise once things get complicated enough, and typedefs can hurt as much as they help.
    The STL handles this by encapsulating manipulations into template algorithms. You could do the same. You're trying to get a begin() vector, and presumably then do something with it. Imagine that this "something" is encapsulated into a function called frob_the_vector(). This function can be made a template:

    Code:
    template <typename T>
    void frob_the_vector(T &vec)
    {
        typename T::const_iterator begin = vec.begin();
        // ... do whatever
    }
    This relieves you of having to determine and fully name the type of "vec," because the template function call mechanism will automatically substitute it for "T" in frob_the_vector<T>().

    And I do understand that sometimes it is difficult to figure out exactly what type some expression has, even if in principle you can figure it out. For instance, boost::lambda functors can have some insanely complicated types.

    Here's a trick to figure out the type of some crazy template type:

    Code:
    int x = expression_of_unknown_type;
    The compiler will barf and spit out an error message like "can't convert from type BLAHBLAHBLAH to integer in assignment," where BLAHBLAHBLAH is the type you're looking for. Very crude, but it works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 6
    Last Post: 07-29-2008, 04:37 AM
  3. strings Vs. Char pointers
    By aijazbaig1 in forum C Programming
    Replies: 49
    Last Post: 02-13-2008, 09:51 AM
  4. "Deciding" in runtime the type of a variable
    By mikahell in forum C++ Programming
    Replies: 28
    Last Post: 07-22-2006, 09:51 AM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM