Class Template Argument Deduction
main.cpp
12345678#include <iostream> #include <utility> // for std::pair int main() { std::pair<int, char> my_pair(1, 'a'); std::cout << my_pair.first << " : " << my_pair.second << std::endl; }
main.cpp
12345678#include <iostream> #include <utility> // for std::pair int main() { std::pair<> my_pair(1, 'a'); // Error std::cout << my_pair.first << " : " << my_pair.second << std::endl; }
main.cpp
123456789101112131415#include <iostream> template <typename T> class Box { T value; public: Box(T value): value(value) {} }; int main() { // No need to write Box<int> a{1}; Box a{1}; // Deduction: Box<int> Box b{a}; // Deduction: Box<int>, not Box<Box<int>> }
vector.h
12345678template <typename T> class Vector3D { T x, y, z; public: Vector3D(T x, T y, T z) : x(x), y(y), z(z) {} }; Vector3D vec{1.0, 2.0, 3.0}; // CTAD deduces Vector3D<double>
¿Todo estuvo claro?
¡Gracias por tus comentarios!
Sección 3. Capítulo 4
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Awesome!
Completion rate improved to 5.88
Class Template Argument Deduction
Desliza para mostrar el menú
main.cpp
12345678#include <iostream> #include <utility> // for std::pair int main() { std::pair<int, char> my_pair(1, 'a'); std::cout << my_pair.first << " : " << my_pair.second << std::endl; }
main.cpp
12345678#include <iostream> #include <utility> // for std::pair int main() { std::pair<> my_pair(1, 'a'); // Error std::cout << my_pair.first << " : " << my_pair.second << std::endl; }
main.cpp
123456789101112131415#include <iostream> template <typename T> class Box { T value; public: Box(T value): value(value) {} }; int main() { // No need to write Box<int> a{1}; Box a{1}; // Deduction: Box<int> Box b{a}; // Deduction: Box<int>, not Box<Box<int>> }
vector.h
12345678template <typename T> class Vector3D { T x, y, z; public: Vector3D(T x, T y, T z) : x(x), y(y), z(z) {} }; Vector3D vec{1.0, 2.0, 3.0}; // CTAD deduces Vector3D<double>
¿Todo estuvo claro?
¡Gracias por tus comentarios!
Sección 3. Capítulo 4