Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6200157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:18:59+00:00 2026-05-24T04:18:59+00:00

The following piece of code #include <iostream> #include <vector> #include <tuple> using namespace std;

  • 0

The following piece of code

#include <iostream>
#include <vector>
#include <tuple>
using namespace std;

class A {
int m_a;

public:
A(): m_a( 10 ) { std::cout << "Default Constructor " << m_a << ", this: " << this << std::endl; }

A( int a ): m_a( a ) { std::cout << "Main Constructor " << m_a << ", this: " << this << std::endl; }

A( const A& a ): m_a( a.m_a) { std::cout << "Copy Constructor: " << m_a << ", this: " << this << std::endl; }

A( const A&& a ): m_a( std::move(a.m_a) ) { std::cout << "RValue copy Constructor " << m_a << ", this: " << this << std::endl; }

int get() const { return m_a; }

virtual ~A() { std::cout << "Destructor " << m_a << ", this: " << this << std::endl; }
};

int main() {
{
    typedef std::tuple< A&& > MyContainer;
    std::vector< MyContainer > myVec;
    {
        A a(100);
        A b(200);

        myVec.push_back( std::make_tuple( a ) );
        myVec.push_back( std::make_tuple( b ) );
        std::cout << "Innermost scope" << std::endl;
    }

    std::cout << "Intermediate scope" << std::endl;
    auto& x = get<0>(myVec.at(0));
    auto& y = get<0>(myVec.at(1));
    std::cout << x.get() << std::endl;
    std::cout << y.get() << std::endl;
}
std::cout << "Outermost scope" << std::endl;
return 0;
}

I expect that, upon leaving the intermeidate scope, the vector destructs and it attempts to destruct the tuples in conatain. This in turn destructs the objects of A, but some how I don’t see any objects getting destroyed. Valgrind doesn’t show any memory errors either. Following is the output when run with valgrind
$valgrind ./a.out

==7366== Memcheck, a memory error detector
==7366== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==7366== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==7366== Command: ./a.out
==7366==
Main Constructor 100, this: 0x7fefffc70
Main Constructor 200, this: 0x7fefffc60
Copy Constructor: 100, this: 0x7fefffcb0
Destructor 100, this: 0x7fefffcb0
Copy Constructor: 200, this: 0x7fefffcd0
Destructor 200, this: 0x7fefffcd0
Innermost scope
Destructor 200, this: 0x7fefffc60
Destructor 100, this: 0x7fefffc70
Intermediate scope
100
200
Outermost scope
==7366==
==7366== HEAP SUMMARY:
==7366==     in use at exit: 0 bytes in 0 blocks
==7366==   total heap usage: 2 allocs, 2 frees, 24 bytes allocated
==7366==
==7366== All heap blocks were freed -- no leaks are possible
==7366== 
==7366== For counts of detected and suppressed errors, rerun with: -v
==7366== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)

I am obviously missing some thing here. If the calls to “get()” in the intermediate scope do return appropriate values (100 and 200), why aren’t the destructors of “A” called when the vector is (and the tuples are) being destructed?

[EDIT]
@Howard Hinnant: Thanks and it makes perfect sense. What I don’t understand are the following:
1) When I construct a tuple with an rvalue and push it to the vector, I do get valgrind memory errors. For eg.

myVec.push_back( std::make_tuple( 10 ) );

in the inner most scope and adding:

auto& z = get<0>(myVec.at(2));
std::cout << z.get() << std::endl;

to the intermediate scope in the above examples produce valgrind memory errors, but not the lvalue references shown above.

2) Could some one please explain why the following statement is calling a copu constructor and an immediate desctructor?

myVec.push_back( std::make_tuple( a ) );
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:19:00+00:00Added an answer on May 24, 2026 at 4:19 am

    ‘a’ and ‘b’ are destructed as you leave the Innermost scope, as your output shows:

    Innermost scope
    Destructor 200, this: 0x7fefffc60
    Destructor 100, this: 0x7fefffc70
    Intermediate scope
    

    The references you obtain via get in the Intermediate scope are dangling: they reference destructed objects. Have ~A() zero m_a to confirm this.

    As you leave Intermediate scope, the vector and the tuples it holds are destructed. The destructor of a tuple of references is a no-op.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following piece of code in C++: #include <iostream> #include <fstream> #include
I have the following piece of code: #include <stdio.h> int main ( int argc,
I have the following piece of code in Visual C++ 2005 : : class
Consider the following piece of Java code. int N = 10; Object obj[] =
I'm using the following piece of Objective-C code to format a NSNumber, and it
I use the following piece of code in an include file. Because it it
The following piece of code shows an Insert table dialog: Dialog d = WordApp.Dialogs[WdWordDialog.wdDialogTableInsertTable];
I have the following piece of code pattern: void M1(string s, string v) {
I have the following piece of code which replaces template markers such as %POST_TITLE%
I used the following piece of code in the service to debug the service

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.