473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

reference_wrapper rationale

1) I was wordering why the reference_wrapper’s [1] constructor is
explicit. For me it seens to be more useful wihout it. I want to
create a “view” of STL containers and the reference_wrapper class is
not so useful in this context.
For example,

int data[] = { 3, 7, 4 };
std::vector<std::tr1::reference_wrapper<int v;

for (int i = 0; i < 3; ++i)
v.push_back(ref(data[i]));

While without the explicit constructor we could do this way:

int data[] = { 3, 7, 4 };
std::vector<std::tr1::reference_wrapper<int v(&data[0], data + 3);

And besides that, the reference object makes more sense to be used as
in:

std::tr1::reference_wrapper<Tref = my_variable;

2) Another usefull feature in the reference_wrapper would be to have:

std::tr1::reference_wrapper<Toperator=(const T& val)
{
*t_ = val;
return *this;
}

For cases when a person holds a container of reference_wrapper and
wants to modify any element:

int data[] = { 3, 7, 4 };
std::vector<std::tr1::reference_wrapper<int v(&data[0], data +
3);

std::replace(v.begin(), v.end(), 7, 30);

[1] http://msdn.microsoft.com/en-us/library/bb982605.aspx.

Nov 11 '08 #1
1 2473
On Nov 11, 12:53*pm, Felipe Farinon <Felipe.Fari...@gmail.comwrote:
1) I was wordering why the reference_wrapper’s [1] constructor is
explicit. For me it seens to be more useful wihout it.
I could not find an explanation, my only speculation is that it is
made explicit so as to protect from inadvertently creating long-lived
references to short-lived data, i.e. dangling references.

--
Max
Nov 12 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Matt Feinstein | last post by:
Hi all-- I'm new to Python, and was somewhat taken aback to discover that the core language lacks some basic numerical types (e.g., single-precision float, short integers). I realize that there...
0
by: Alexander Grigoriev | last post by:
I hope Mr. Stroustrup can give an answer to this question: What was rationale behind the requirements to use an ampersand and a fully qualified name of a function, to form a pointer to a member...
2
by: Howard Jess | last post by:
Given the html at the end of this message, I see how a DOM NodeList exhibits its "live" behavior; that is, adding elements to a document can change any NodeList variables, when there's *no* code...
8
by: Robert Latest | last post by:
Hello, I'm new in javascript programming (but am quite literate in HTML, CSS, and C). It's amazing what one can do with JS and fairly modern browsers. One thing that struck me as odd was that...
17
by: Janice | last post by:
char* line = "abcd"; How to convert the line to upper case and print? Any option for printf to do this? Thanx
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
15
by: Ben Hinkle | last post by:
I'm curious, what was the rationale for making a builtin type _Bool but then having #define true 1 #define false 0 in stdbool.h? That seems very odd that true and false don't have type _Bool. In...
2
by: Emmanuel Deloget | last post by:
Hello, I'm trying to find a (sfinae powered) way to verify if a particular type declares a subtype (either using typedef or by declaring a subclass). To be more concrete, let's say that I'm...
1
by: Nick Bastin | last post by:
I've done some searching in this newsgroup and on the internet and haven't found an answer, so I thought I'd ask here... :-) I don't understand why full specialization of a member function...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.