Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Class Template Argument Deduction | Class Templates
C++ Templates

bookClass Template Argument Deduction

main.cpp

main.cpp

copy
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

main.cpp

copy
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

main.cpp

copy
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

vector.h

copy
12345678
template <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>
question mark

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 4

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Awesome!

Completion rate improved to 5.88

bookClass Template Argument Deduction

Desliza para mostrar el menú

main.cpp

main.cpp

copy
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

main.cpp

copy
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

main.cpp

copy
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

vector.h

copy
12345678
template <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>
question mark

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 4
some-alt