lundi 27 mars 2023

Since brace-initializataion is possible, why do we still have std::make_pair? What's the purpose or advantage of it? [duplicate]

As we know, both of these ways to assign are possible. Why we still have std::make_pair?

std::pair<int, char> myPair1 = { 1,'a' };
std::pair<int, char> myPair2 = std::make_pair(1, 'a');

I'm not here to criticize the syntax. I just really want to know what's the advantage for us to use std::make_pair instead of directly using the curly braces.

Actually, I think the only advantage is that it can make our code more readable, like you're passing the parameter with std::make_pair instead of the curly braces. It makes it easier to tell that you're passing a pair. Since the curly brace can refer to something more, like a tuple or array.

Aucun commentaire:

Enregistrer un commentaire