Skip to content

Type index

DryPerspective edited this page Sep 21, 2023 · 4 revisions

A recreation of <type_index> - copyable type which contains the information provided by std::type_info. A dp::type_index can be copied, and can be compared according to the implementation-defined type_info comparison order.

No hashing support is provided, as std::hash did not exist until C++11, and I have not implemented hashing anywhere.

Feature

type_index A copy-constructble, copy-assignable type which stores type information

Sample code

#include <iostream>
#include <set>

#include "cpp98/iterator.h"
#include "cpp98/type_index.h"

int main(){

    //A set will internally reorder its contents
    std::set<dp::type_index> types;
    types.insert(typeid(int));
    types.insert(typeid(double));
    types.insert(typeid(std::string));
    types.insert(typeid(std::vector<int>));
    types.insert(typeid(char));
    types.insert(typeid(int));  //Duplicate will be detected here and only one typeid(int) index will remain in the set

    typedef typename dp::const_iterator_type<std::set<dp::type_index> >::type Iter;
    for(Iter it = dp::begin(types); it != dp::end(types); ++it){
        std::cout << it->name() << ' ';
    }

}

Clone this wiki locally