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

  • SEARCH
  • Home
  • 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 8289945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:38:57+00:00 2026-06-08T12:38:57+00:00

Update: See the full answer below. The short answer is no, not directly. You

  • 0

Update: See the full answer below. The short answer is no, not directly. You can create an indirect reference using std::reference_wrapper or accomplish the same effect more generally with pointers (but without the syntactic sugar and added safety of references).

I ask because tuples make a convenient variadic storage unit in C++11. In theory it sounds reasonable for one element of a tuple to hold a reference to another element in the same tuple. (Replace “reference” with “pointer” and it works in practice.) The devil is the details of constructing such a tuple. Consider the following example:

#include <tuple>
#include <iostream>

class A
{
public:
  A() : val(42) { }
  int val;
};

class B
{
public:
  B(A &a) : _a(a) { }
  int val() { return _a.val; }

private:
  A &_a;
};

int main()
{
  A a;
  B b(a);
  std::tuple<A, B> t1(a, b);
  a.val = 24;
  std::cout << std::get<0>(t1).val << "\n"; // 42
  std::cout << std::get<1>(t1).val() << "\n"; // 24

  return 0;
}

The second element in the tuple t1 references the automatic variable a instead of the first element in t1. Is there any way to construct a tuple such that one element of the tuple could hold a reference to another element in the same tuple? I’m aware that you could sort of achieve this result by creating a tuple of references, like this:

int main()
{
  A a;
  B b(a);
  std::tuple<A &, B &> t2 = std::tie(a, b);
  a.val = 24;
  std::cout << std::get<0>(t2).val << "\n"; // 24
  std::cout << std::get<1>(t2).val() << "\n"; // 24

  return 0;
}

But for my purposes that’s cheating, since the second element in t2 is still ultimately referencing an object that lives outside of the tuple. The only way I can think of doing it compiles fine but may contain undefined behavior [Edited to reflect more concise example provided by Howard Hinnant]:

int main()
{
    std::tuple<A, B> t3( A(), B(std::get<0>(t3)) ); // undefined behavior?
    std::get<0>(t3).val = 24;
    std::cout << std::get<0>(t3).val << "\n";
    std::cout << std::get<1>(t3).val() << "\n"; // nasal demons?
}

Edit: Here is a minimal test program that returns with a non-zero exit status when compiled using g++ 4.7 with -O2 or higher. This suggests either undefined behavior or a bug in gcc.

#include <tuple>

class Level1
{
public:
  Level1() : _touched(false), _val(0) { }

  void touch()
  {
    _touched = true;
  }

  double feel()
  {
    if ( _touched )
    {
      _touched = false;
      _val = 42;
    }
    return _val;
  }

private:
  bool _touched;
  double _val;
};

class Level2
{
public:
  Level2(Level1 &level1) : _level1(level1) { }

  double feel()
  {
    return _level1.feel();
  }

private:
  int _spaceholder1;
  double _spaceholder2;
  Level1 &_level1;
};

class Level3
{
public:
  Level3(Level2 &level2) : _level2(level2) { }

  double feel()
  {
    return _level2.feel();
  }

private:
  Level2 &_level2;
};

int main()
{
  std::tuple<Level3, Level2, Level1> levels(
    Level3(std::get<1>(levels)),
    Level2(std::get<2>(levels)),
    Level1()
  );

  std::get<2>(levels).touch();

  return ! ( std::get<0>(levels).feel() > 0 );
}
  • 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-06-08T12:38:59+00:00Added an answer on June 8, 2026 at 12:38 pm

    This works for me:

    #include <tuple>
    #include <iostream>
    
    int main()
    {
        std::tuple<int&, int> t(std::get<1>(t), 2);
        std::cout << std::get<0>(t) << '\n';
        std::get<1>(t) = 3;
        std::cout << std::get<0>(t) << '\n';
    }
    

    Update

    I just asked about this case on the CWG mailing list. Mike Miller assures me that this is undefined behavior per 3.8p6 bullet 2:

    … The program has undefined behavior if:

    …

    • the glvalue is used to access a non-static data member or call a non-static member function of the object, or

    …

    It would be well defined behavior if tuple was an aggregate, but because tuple has a user-declared constructor, 3.8p6b2 applies.

    However this works, and avoids UB:

    #include <tuple>
    #include <functional>
    #include <cassert>
    
    int main()
    {
        int dummy;
        std::tuple<std::reference_wrapper<int>, int> t(dummy, 2);
        std::get<0>(t) = std::get<1>(t);
        assert(std::get<0>(t) == 2);
        std::get<1>(t) = 3;
        assert(std::get<0>(t) == 3);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE see below I am not sure how to populate a list or vector
UPDATE: This is now resolved, see answer below. On one of my forms (in
UPDATE (spoiler): This question is answered (see David Carlisle answere below) and it looks
Figured it out, see Update below. I'm trying to work with a particular web
Please have a look at the schema below: CREATE TABLE Person (id int not
I've tried Django-registration, see this tutorial to create a full Login-system. In the tutorials
Update: See the bottom of this question for a C# workaround. Hi there, Consider
(Please see update at bottom) I've looked at dozens of questions and haven't been
OK, I thought I would try one last update and see if it gets
Update: OK I see it's a bubble sort, but is it less efficient because

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.