Class type from pointer used as template argument

If a pointer to a user-defined type is passed as a template argument to the template class, is it possible to get the class type of the argument?

template <class T> struct UserType {
    typedef T value_type;
    ...
};

int main () {
    typedef std::vector<UserType<double>*> vecType
    vecType vec;

    vecType::value_type::value_type m; //how to get the double here?

    return 0;
}
+3
source share
4 answers

Use features:

template <typename> struct ptr_traits {};
template <typename T> struct ptr_traits<T*>
{ typedef T value_type; };

ptr_traits<vecType::value_type>::value_type m;
+5
source

- , , .

typedef vecType m:

typedef UserType<double>* UserTypeDoublePtr;
typedef std::vector<UserTypeDoublePtr> vecType;

UserTypeDoublePtr m;
+2

, "" ?

, , ( ), .

?

, 100% - . , + / . .

, , . :

template <typename T> const char* TypeName();
template <> const char* TypeName<int>() { return "int"; }
template <> const char* TypeName<double>() { return "double"; }
// ...

, TypeName , . TypeName.

0
source

Source: https://habr.com/ru/post/1754385/


All Articles