tags:

views:

180

answers:

3

Hello

Is it valid to have a std::pair of references ? In particular, are there issues with the assignment operator ? According to this link, there seems to be no special treatment with operator=, so default assignement operator will not be able to be generated.

I'd like to have a pair<T&, U&> and be able to assign to it another pair (of values or references) and have the pointed-to objects modified.

+2  A: 

You are right. You can create a pair of references, but you can't use operator = anymore.

ybungalobill
+4  A: 

I think it would be legal to have a std::pair housing references. std::map uses std::pair with a const type, after all, which can't be assigned to either.

I'd like to have a pair<T&, U&> and be able to assign to it another pair

Assignment won't work, since you cannot reset references. You can, however, copy-initialize such objects.

sbi
@sbi - is reference to a reference ever legal? Wouldn't that be required if using the pair in many places in STL?
Steve Townsend
I don't want to reset references. I'd like the *referenced* objects to be assigned.
Alexandre C.
@Alexandre: There will be no compiler-generated assignment operator for classes with reference member. But you can always derive from `std::pair` and give the derived class its own `operator=()` which does what you want.
sbi
sbi
@sbi: I like this solution. I give it a try.
Alexandre C.
Steve Townsend
@Steve: In a template, if you have a type `T` that is instantiated with a reference type (say, `int it is certainly in C++0x, though the rules are a bit more complex due to the introduction of rvalue references.
James McNellis
@James McNellis - perfect, thanks. If this was an answer I would upvote.
Steve Townsend
@Steve Townsend: reference to reference does not make sense. A reference is not a variable in that type of sense it is an alternative name for an existing variable (ie and Alias). So a reference **`IS`** the other variable just named differently.
Martin York
@Martin York - thanks, this is all becoming a little clearer. I found an in-depth discussion to clarify this further for myself.
Steve Townsend
Alexandre, according to James and Johannes, my answer is wrong. `:(` You should pick on of theirs instead.
sbi
@sbi: MSVC has had this behaviour for as long as I can remember, and it's definitely in C++0x. Won't speak for C++03 but I thought it was Standard behaviour.
DeadMG
+5  A: 

No, you cannot do this reliably in C++03, because the constructor of pair takes references to T, and creating a reference to a reference is not legal in C++03.

Notice that I said "reliably". Some common compilers still in use (for GCC, I tested GCC4.1, @Charles reported GCC4.4.4) do not allow forming a reference to a reference, but more recently do allow it as they implement reference collapsing (T& is T if T is a reference type). If your code uses such things, you cannot rely on it to work on other compilers until you try it and see.

It sounds like you want to use boost::tuple<>

int a, b;

// on the fly
boost::tie(a, b) = std::make_pair(1, 2);

// as variable
boost::tuple<int&, int&> t = boost::tie(a, b);
t.get<0>() = 1;
t.get<1>() = 2;
Johannes Schaub - litb
Now when and where did the map come into this?
sbi
@sbi thanks. I knew i had too little sleep -.-
Johannes Schaub - litb
@Johannes: What, at 8:15pm?? Now, when I was a student... `:)`
sbi
@sbi yep, all day long hard work at company. Now all my power is gone :(
Johannes Schaub - litb
I have no idea why someone would downvote this. People, I don't care who was first to mention this fact in some comment elsewhere. All I care is that the correct answer be made as *answer*. Not as *deleted answer* or *comment*.
Johannes Schaub - litb
Well, in all fairness, this doesn't answer the question asked, so it is a bad answer.
sbi
@sbi The question was "Is it valid to have a std::pair of references ? I want ..." and I answered correctly "No you cannot do this reliably in C++03".
Johannes Schaub - litb
Oh, I still read "map", where you have now written "pair". Then your answer is indeed right, and the down-vote wrong.
sbi
Anyway, IIUC, then you're saying that the reference collapsing feature wasn't in C++03? __Edit:__ Ah, I see, James and you have already sorted that out.
sbi
Hmmm, I've just tested gcc 4.4.4 (more recent that 4.1) and it correctly disallows forming a reference to a reference.
Charles Bailey