কম্পিউটার টিউটোরিয়াল

C++-এ টাইপ ইনফারেন্স (Type Inference)


Type inference or deduction refers to the automatic detection of the data type of an expression in a programming language. It is a feature present in some strongly statically typed languages. In C++, the auto keyword(added in C++ 11) is used for automatic type deduction. For example, you want to create an iterator to iterate over a vector, you can simply use auto for that purpose.

টাইপ ইনফারেন্স বা ডিডাকশন বলতে প্রোগ্রামিং ল্যাঙ্গুয়েজে এক্সপ্রেশনের ডেটা টাইপ অটোমেটিক্যালি ডিটেক্ট কাপার বুঝায়। এটি কিছু স্ট্রংলি স্ট্যাটিক্যালি টাইপড ল্যাঙ্গুয়েজে ফিচার হিসেবে থাকে। C++-এ, auto কীওয়ার্ড (C++11-এ যোগ করা) অটোমেটিক টাইপ ডিডাকশনের জন্য ব্যবহৃত হয়। উদাহরণস্বরূপ, আপনি একটি ভেক্টর ওইটারেট করার জন্য একটি ইটারেটর তৈরি করতে চান, আপনি সিম্পলি সেটির জন্য auto ব্যবহার করতে পারেন।

উদাহরণ

#include <iostream> #include <vector> using namespace std; int main() {    vector<int> arr(10);    for(auto it = arr.begin(); it != arr.end(); it ++) {       cin >> *it;    }    return 0; }

আউটপুট

উপরোক্ত প্রোগ্রামে, it অটোমেটিক্যালি std::vector<int>::iterator টাইপ পাবে।

  1. C++-এ কনস্ট্রাক্টরে ভেক্টর পাস করা (Passing a vector to constructor in C++)

  2. C++-এ vector::resize() বনাম vector::reserve() পার্থক্য

  3. C++-এ টাইপ ইনফারেন্স (Type Inference) কী?

  4. C++-এ টাইপ ডিডাকশন (Type Deduction) কী?