0

I've got iterator through undefined type:

 for (typename Type::const_iterator hayStackIterator = hayHeap.begin(); hayStackIterator != hayHeap.end(); ++hayStackIterator) {
           //some inner logic
}

and it would be nice, to know what type is my *hayStackIterator to be able to modify the inner logic pursuant this information... Is there some simple function to make something like this?

if (*hayStackIterator.isInstanceOf(vector<string>){
//do something
} else if (*hayStackIterator.isInstanceOf(string){
//do something else
}

I can use these includes:

#include <cctype>
#include <iostream>
#include <iomanip>
#include <set>
#include <list>
#include <map>
#include <vector>
#include <queue>
#include <string>
Martin Dvoracek
  • 1,714
  • 6
  • 27
  • 55
  • Have you tried std::is_same? – derpface Apr 20 '13 at 03:45
  • I'm quite new to C++, so I don't know the possibilites, that it offers...I'll look at this :) – Martin Dvoracek Apr 20 '13 at 03:46
  • What you can do depends entirely on the implementation of the Type class, and you've told us absolutely nothing about it. For example, C++'s inbuilt run-time polymorphic dispatch (virtual functions), boost::variant and boost::any all have different ways of handling multiple types. – Tony Delroy Apr 20 '13 at 04:11

2 Answers2

3

Put the inner logic in a function, and overload that function:

void innerLogic(vector<string> const& vec) {
    //do something
}

void innerLogic(string const& str) {
    //do something else
}

void loop() {
    for (typename Type::const_iterator hayStackIterator = hayHeap.begin();
         hayStackIterator != hayHeap.end();
         ++hayStackIterator)
    {
        innerLogic(*hayStackIterator);
    }
}
Mankarse
  • 39,818
  • 11
  • 97
  • 141
  • 1
    Because of such necessary code, sometimes I feel C++1y must introduce *overloadable* local functions. – Nawaz Apr 20 '13 at 03:55
  • What's this supposed to do? `Type::const_iterator::operator*` has to have a single return type - it's not going to return distinct types each time that you can dispatch with compile-time polymorphism to overloaded functions. Am I missing something? – Tony Delroy Apr 20 '13 at 04:10
  • @TonyD: The assumption is that `Type` is dependent on a template parameter and so can change at compile time, which will lead to a different return type from `operator*`, which will in turn dispatch to a different overload of `innerLogic`. – Mankarse Apr 20 '13 at 04:27
  • Although, I must admit that I'm surprised that the author of [this answer](http://stackoverflow.com/a/5854862/485561) would be asking this question. Am I missing something? – Mankarse Apr 20 '13 at 04:33
  • On my initial reading of the question, I thought the iteration was over a set of different but unknown types (i.e. that something like C# Object/is instance was wanted - like forcing everything to derive from an empty Object class and using RTTI/dynamic_cast to test for runtime type). Your code seems to be assuming that Type will be one of a known set of types. Rereading the question, well - who knows lol. Thanks for explaining where you were coming from. Cheers. – Tony Delroy Apr 20 '13 at 06:29
1

You can use the typedefs from the STL containers to help you with SFINAE.

template<typename Type> 
function flunk(const Type& hayHeap) {

    typedef typename Type::value_type inner_type;   // STL container typedef
                     ^^^^^^^^^^^^^^^^
    // if Type = vector<string> -> inner_type = string
    // if Type = string         -> inner_type = char

    inner_type start = inner_type();  // initialize start to default.

    for (typename Type::const_iterator hayStackIterator = hayHeap.begin(); hayStackIterator != hayHeap.end(); ++hayStackIterator) {
               //some inner logic
    }

}
stardust
  • 5,918
  • 1
  • 18
  • 20