dimanche 9 janvier 2022

How Does std::cout << 5 << std::endl << 6; Work?

I am trying to understand that even though std::endl is a function template then how can we use it in a statement like:

std::cout << 5 << std::endl << 6;

My question is that:

Even though std::endl is a function template and we call any function using call operator () and we can also take address of a function using & but in the example above we have done neither. That is, neither we have applied the & to std::endl nor we have called it like std::endl(). So how does this work.

For example, if we have a function template as follows:

template<typename T> T func(T t)
{
    return t;
}

Now i want to call the function template func<> then i can do it like: func(10); or i can also write

template<typename T> T func(T t)
{
    return t;
}
int main(){
    
    std::string (*ptr)(std::string)  = &func;
    std::string (*ptr2)(std::string) = func;//equivalent to above statement
}

As in the 2nd case of the above example, we have not explicitly used &. So when we use std::endl is the & implicitly applied just like in the second case of my above example.

PS: I have read this before asking this question.

Aucun commentaire:

Enregistrer un commentaire