Understand parameter type in void f (const T & param)

Link: Effective modern C ++. Item 4.

https://github.com/BartVandewoestyne/Effective-Modern-Cpp/blob/master/Item04_Know_how_to_view_deduced_types/runtime_output02.cpp

class Widget {};

template<typename T>                // template function to
void f(const T& param)              // be called
{
}

std::vector<Widget> createVec()    // factory function
{
    std::vector<Widget> vw;
    Widget w;
    vw.push_back(w);
    return vw;
}

int main()
{
    const auto vw = createVec();        // init vw w/factory return

    if (!vw.empty()) {
      f(&vw[0]);                        // call f
      // ...
    }
}

Based on the book as a type T, and paramis as follows:

T = class Widget const *
param = class Widget const * const &

I have problems to understand why param- this type with the above f(const T& param).

Here is my understanding

T = class Widget const *

So, f(const T& param)it becomes the following:

f(const const Widget * & param).

Why is the real type paramsmaller Widget const * const &?

+4
source share
2 answers

s > Widget const * const ?

, ... T - , const T & ( T const &, , const ) T, .

, cont *.

: const T & T const &; T, const Widget *, T const & const Widget * const & , , Widget const * const &

+2

const const ( const const).

* const. Widget const * const (Widget), Widget * const const -const (Widget), Widget const * const const const (Widget).

T = Widget const *, const; , const T, const T , , , const T Widget const * const.

+2

Source: https://habr.com/ru/post/1666062/


All Articles