← Back to list

C++11 ‘auto’ specifier

It is basically a placeholder for an actual type. That means the compiler reserves a type for a variable or function which have yet to be…

Developer Notes · 2023-12-29 21:48 · 1 claps · 2.0 min read
#auto-specifier #c-17 #type-deduction #cpp #cpp-programming
Open on Medium ↗
Wiki topics: 💻 · Programming

C++11 ‘auto’ specifier

[embed]

It is basically a placeholder for an actual type. That means the compiler reserves a type for a variable or function which have yet to be finalised.

Auto is basically an automatic type deduction mechanism.

#include <iostream>
#include <chrono>
#include <atomic>
#include <type_traits>
#include <vector>
#include <memory>

int i = 5;
int &get_num() { return i; }

class parentAuto
{
private:
    int num;
    static constexpr auto num2 = 8.9;
};

int main()
{
    {
        auto j = 4;
        std::cout << j << "\t" << typeid(j).name() << std::endl;

        auto double_num = 2.5;
        std::cout << double_num << "\t" << typeid(double_num).name() << std::endl;

        auto float_num = 2.589;
        std::cout << float_num << "\t" << typeid(float_num).name() << std::endl;

        auto PKc = "developer";
        std::cout << PKc << "\t" << typeid(PKc).name() << std::endl;

        auto str = std::string{"developer"};
        std::cout << str << "\t" << typeid(str).name() << "\n" << std::endl;
    }

    {
        auto j = 4;
        auto var2 = static_cast<std::uint64_t>(j);
        std::cout << var2 << "\t" << typeid(var2).name() << "\n" << std::endl;
    }

    {
        auto arr = new char[5]{0};
        std::cout << arr << "\t" << typeid(arr).name() << std::endl;

        auto ptr = std::make_shared<int>(5);
        std::cout << ptr << "\t" << typeid(ptr).name() << std::endl;

        auto add = [](auto num1, auto num2)
        { return num1 + num2; };
        auto result = add(5, 6);
        std::cout << result << "\t" << typeid(result).name() << std::endl;

        auto result_str = add(std::string{"developer"}, std::string{"notes"});
        std::cout << result_str << "\t" << typeid(result_str).name() << "\n" << std::endl;
    }

    {
        auto ilist = {3, 4, 5};
        std::cout << typeid(ilist).name() << std::endl;

        auto vec = std::vector<int>{3, 4, 5};
        std::cout << typeid(vec).name() << std::endl;

        for (auto itr = vec.begin();
             itr != vec.end(); itr++)
        {
            std::cout << typeid(itr).name() << std::endl;
            // Do your coding
        }

        auto size = vec.size();
        std::cout << typeid(size).name() << "\n" << std::endl;
    }

    {
        auto j = 4;
        auto &j_ref = j;
        std::cout << j << "\t" << typeid(j_ref).name() << std::endl;

        auto double_num = 2.5;
        auto &double_num_ref = double_num;
        std::cout << double_num << "\t" << typeid(double_num_ref).name() << std::endl;

        auto &res = get_num();
        std::cout << res << "\t" << typeid(res).name() << std::endl;

        const auto now = std::chrono::system_clock::now();
        std::cout << typeid(now).name() << "\n" << std::endl;
    }

    // The below code works only in C++17
    auto j = 4;
    auto var2 = static_cast<std::atomic_uint64_t>(j);
    std::cout << var2 << "\t" << typeid(var2).name() << std::endl;

    return 0;
}

Output

4 i
2.5 d
2.589 d
developer PKc
developer NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

4 m

 Pc
0x1da92f0 St10shared_ptrIiE
11 i
developernotes NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

St16initializer_listIiE
St6vectorIiSaIiEE
N9__gnu_cxx17__normal_iteratorIPiSt6vectorIiSaIiEEEE
N9__gnu_cxx17__normal_iteratorIPiSt6vectorIiSaIiEEEE
N9__gnu_cxx17__normal_iteratorIPiSt6vectorIiSaIiEEEE
m

4 i
2.5 d
5 i
NSt6chrono10time_pointINS_3_V212system_clockENS_8durationIlSt5ratioILl1ELl1000000000EEEEEE

4 St6atomicImE

Click here to debug the above program

Why auto C++11 specifier is needed?

  1. It doesn’t allow the variable declaration without initialisation.

2. The more generic the code means the more open to future changes.

3. Consistent coding style.

4. It saves time in typing long names and remembering the names.

5. No more implicit conversion.

Where auto can’t be used?

  1. Not a placeholder for const, volatile, & reference specifiers.

2. auto can’t be used as types that are not moveable.

3. ‘auto’ can’t be used in the class member types because mostly the members will not be defined in the class.


메타데이터
post_id
2c5f017af94f
slug
c-11-auto-specifier-2c5f017af94f
url
https://medium.com/@developer_notes/c-11-auto-specifier-2c5f017af94f
canonical_url
https://medium.com/@developer_notes/c-11-auto-specifier-2c5f017af94f
author_url
https://medium.com/@developer_notes
status
ok
fetched_at
2026-06-29 01:02:39