6
 int main() {

   pair<int, int> res;
   // check here pair is empty ?
   // res = make_pair(1 , 2);


   return 0;
}

In the above snippet what is the ideal way to check if a pair has been initilaized or not?

EDIT: As pointed out in some answers below, using the word "uninitialized" is wrong here, to be more specific how do I check if the value has been explicitly set (other than the default constructor)

4
  • 4
    Looks like a XY problem Commented Jul 19, 2019 at 9:11
  • You look at your program. Is there initialisation somewhere? Then it is initialised. No initialisation? Not initialised. In your case, there is initialisation in the std::pair constructor. Since there is no such thing as empty pair, the check here is trivial. Commented Jul 19, 2019 at 9:16
  • @CinCout I wonder, in order for this not to be an XY Problem the mvce should be about a general pair<T, U> instead of the specific pair<int, int>, right? Commented Jul 19, 2019 at 9:42
  • The OP might have dumbed it down. One can only guess! Commented Jul 19, 2019 at 9:45

4 Answers 4

9

There's no such thing as unitialized std::pair.

Default constructor for std::pair will value-initialize both it's members, which for pair of ints means both int will have value 0.

Sign up to request clarification or add additional context in comments.

Comments

8

In the above snippet what is the ideal way to check if a pair has been initilaized or not?

You check your favorite language reference and realize that

  • std::pair requires default-constructible types and

  • the std::pair default constructor default-initializes its data members.

And hence,

std::pair<int, int> res;

will result in res.first and res.second both be zero.


When you need to add an additional empty state to an object, it's always an option to explicitly wrap it into a std::optional (or boost::optional if you can't use C++17). This way, you can always check whether you have a meaningful std::pair or not:

#include <optional>

std::optional<std::pair<int, int>> res;

if (res) // the pair is initialized and usable
   doStuff(*res);
else // ... it's not, hence initialize it
   res = std::make_pair(42, 43);

Comments

4

You can write a function similar to the following

namespace usr
{
    template <class T1, class T2>
    bool empty( const std::pair<T1, T2> &p )
    {
        return p == std::pair<T1, T2>();
    }
}

That is a default initialized pair will be considered as ampty.

Here is a demonstrative program

#include <iostream>
#include <utility>

namespace usr
{
    template <class T1, class T2>
    bool empty( const std::pair<T1, T2> &p )
    {
        return p == std::pair<T1, T2>();
    }
}

int main()
{
    std::pair<int, int> p1( 1, 2 );

    std::cout << usr::empty( p1 ) << '\n';

    std::pair<int, int> p2;

    std::cout << usr::empty( p2 ) << '\n';
}

Its output is

0
1

Comments

3

This depends on how you define the std::pair is empty. The default constructor of std::pair would value-initialize both elements of the pair, that means for pair<int, int> res;, its first and second would be initialized to 0. That's the only way you can check for a default-constructed std::pair, if they're guaranteed to be non-zero after the assignment.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.