Frage

Hier ist, was würde ich tun:

  • Aus boost::any würde Ich mag es wissen, ist ein Zeigertyp.
  • Wenn es ein Zeiger ist, ich Klon haben es

So etwas wie folgt aus:

boost::any value= new vector<string>();

if (typeid(value).IsPointerType())
{
  boost::any newValue = Clone(value);
}

Glauben Sie, dass es möglich ist?

Danke für Ihre Hilfe

NB: Ich brauche dies für einen Rahmen, der Lage sein sollte, Standardwert zu initialisieren

.
War es hilfreich?

Lösung

Sie können die type_info Schnittstelle verwenden:

#include <boost/any.hpp>
#include <iostream>
#include <typeinfo>

using namespace std;

int main()
{
    boost::any intVal = 5;

    int* a = new int(6);
    boost::any ptrVal = a;

    cout << intVal.type().__is_pointer_p() <<endl;
    cout << ptrVal.type().__is_pointer_p() << endl; 

    return 0;
}

Returns

0
1

Andere Tipps

Sie könnte so etwas wie diese verwenden (habe es nicht kompilieren):

#include <boost/type_traits.hpp>

class any_p: public boost::any {
    const bool is_ptr_;
public:
    template<class T>
    any_p(T obj): boost::any(obj), is_ptr_(is_pointer<T>::value_type) {}
    const bool is_ptr() const { return is_ptr_; }
};
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top