4

I want to re-seat a pair of references refPair

int a, b, c, d;
pair<int&, int&> refPair(a, b);

Doing this seems to cause values of c and d to be copied to a and b, which I don't want

refPair = pair<int&, int&>(c, d);

Doing this however doesn't

new(&refPair) pair<int&, int&>(c, d);

I want to know if this is legal and doesn't cause any undefined behavior. It works fine with my compiler but I'm not sure if its portable.

user1353535
  • 651
  • 1
  • 5
  • 18
  • 3
    I can't comment on it being legal, but the whole point of having your pair contain references is that they aren't supposed to change. So to forcibly change the contents of your pair when it's not supposed to change, whether legal or not, is definitely on the lines of "you shouldn't do that". Why are you not using `pair` instead - then you can change your content to your hearts content. – Mats Petersson Jan 27 '13 at 16:23
  • I want to avoid null values and dont want to use indirection operator to dereference it everytime. – user1353535 Jan 27 '13 at 16:25
  • 1
    Read about `std::reference_wrapper`. – Pete Becker Jan 27 '13 at 16:27
  • Well, so add a function to set your pair that checks for NULL and aborts if NULL is passed in. Sorry, can't solve the "dereference operator" - it will be an indirect operation once compiled either way, but yes, you do have to type at least one more character in the code. – Mats Petersson Jan 27 '13 at 16:27

2 Answers2

4

I'm fairly certain that is undefined behavior, as it's illegal to construct over non-trivial classes like that (std::pair can be non-trivial AFAIK).

Anyway, look into std::reference_wrapper which is reseatable.

refPair = pair<std::reference_wrapper<int>, std::reference_wrapper<int> >(std::ref(c), std::ref(d));
Pubby
  • 51,882
  • 13
  • 139
  • 180
  • Thanks, this solves most of my needs. However the only thing bugging me is having to use `get()` function to reassign values. – user1353535 Jan 27 '13 at 16:40
  • 1
    It is legal to take a buffer and construct and destruct objects many times in the same place. How close that can be made to the OP's code... – Marc Glisse Jan 27 '13 at 16:42
  • Note that in most cases it's pretty silly to use `reference_wrapper` (which contains a pointer) instead of using pointers directly; the wrapper is most often just extra work and real ugly notation. `reference_wrapper` was nice for forwarding arguments in C++03. but then it wasn't part of the standard library, so, a bit of catch 22... – Cheers and hth. - Alf Jan 27 '13 at 16:43
  • @MarcGlisse so assuming i call `refPair.pair::~pair()`, should that be ok? – user1353535 Jan 27 '13 at 16:46
  • @user1353535: you just need to learn the basics of C++. nothing needs to be as complicated and ugly as the "solutions" you propose. to problems that don't exist. get yourself **[a textbook](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)**. – Cheers and hth. - Alf Jan 27 '13 at 16:52
  • @Cheersandhth.-Alf the reference wrapper solution works pretty well. I find pointers and dereferencing uglier. And also i shouldve mentioned in my question I wasn't asking for opinions on what is ugly code or not. I am asking for potential problems with it if any and in which situations it may arise because I havent faced any. – user1353535 Jan 27 '13 at 17:01
0

You write,

“ want to re-seat a pair of references refPair”

No, references can't be reseated.

Technically you could use pointers instead.

But what your original code does is meaningless. And what your amended "working" code does is meaningless and formally ungood. Chances are therefore that this is an XY question, i.e. where you're asking about the shortcomings of an imagined solution Y instead of the real problem X – which is what?

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331