Cómo escribir una clase de plantilla? [duplicar]
Esta pregunta ya tiene una respuesta aquí:
- C++ template typedef 2 respuestas
¿Cómo debería typedef
a template class
? Algo como:
typedef std::vector myVector; // <--- compiler error
Sé de 2 maneras:
(1) #define myVector std::vector // not so good
(2) template<typename T>
struct myVector { typedef std::vector<T> type; }; // verbose
¿Tenemos algo mejor en C++0x ?
67
2 answers
Sí. Se llama " plantilla de alias", y es una nueva característica en C++11.
template<typename T>
using MyVector = std::vector<T, MyCustomAllocator<T>>;
El uso sería entonces exactamente como usted espera:
MyVector<int> x; // same as: std::vector<int, MyCustomAllocator<int>>
GCC lo ha soportado desde 4.7 y Clang lo tiene desde 3.0.
115
Author: Travis Gockel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-03-25 20:26:38
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-03-25 20:26:38
En C++03 se puede heredar de una clase (pública o privada) para hacerlo.
template <typename T>
class MyVector : public std::vector<T, MyCustomAllocator<T> > {};
Necesita hacer un poco más de trabajo (Específicamente, constructores de copia, operadores de asignación), pero es bastante factible.
15
Author: dascandy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-02 07:00:53
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-02 07:00:53