473,385 Members | 1,693 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,385 software developers and data experts.

Qualifiers lost compiling STL code

Given the following code:

#include <vector>
#include <algorithm>
#include <functional>

class Value
{
public:
explicit Value(int value) : m_value(value) {}
void ChangeTo(int value) { m_value = value; }

private:
int m_value;
};

int main()
{
std::vector<Value> values;
values.insert(values.end(), 5, Value(6));

std::for_each(values.begin(), values.end(),
std::bind2nd(std::mem_fun_ref(&Value::ChangeTo), 7));

return 0;
}

Under two different compiler, the code compiles without any warnings
or errors. A third compiler produces an error with output like the
following:

error: 'std::mem_fun1_ref_t<void,class Value,int>::operator`()'' :
cannot convert parameter 1 from 'const
std::binder2nd<_Fn2>::argument_type' to 'Value &' with
[
_Fn2=std::mem_fun1_ref_t<void,Value,int>
]
Conversion loses qualifiers while compiling class-template member
function 'std::binder2nd<_Fn2>::result_type
std::binder2nd<_Fn2>::operator ()(const
std::binder2nd<_Fn2>::argument_type &) const' with
[
_Fn2=std::mem_fun1_ref_t<void,Value,int>
]
see reference to class template instantiation 'std::binder2nd<_Fn2>'
being compiled with
[
_Fn2=std::mem_fun1_ref_t<void,Value,int>
]

Is this a valid error?
What can I do to fix the problem?
Jul 22 '05 #1
3 2061
In article <a4**************************@posting.google.com >,
tr*********@verizon.net (Tron Thomas) wrote:
Given the following code:

#include <vector>
#include <algorithm>
#include <functional>

class Value
{
public:
explicit Value(int value) : m_value(value) {}
void ChangeTo(int value) { m_value = value; }

private:
int m_value;
};

int main()
{
std::vector<Value> values;
values.insert(values.end(), 5, Value(6));

std::for_each(values.begin(), values.end(),
std::bind2nd(std::mem_fun_ref(&Value::ChangeTo), 7));

return 0;
}

Under two different compiler, the code compiles without any warnings
or errors. A third compiler produces an error with output like the
following:

error: 'std::mem_fun1_ref_t<void,class Value,int>::operator`()'' :
cannot convert parameter 1 from 'const
std::binder2nd<_Fn2>::argument_type' to 'Value &' with
[
_Fn2=std::mem_fun1_ref_t<void,Value,int>
]
Conversion loses qualifiers while compiling class-template member
function 'std::binder2nd<_Fn2>::result_type
std::binder2nd<_Fn2>::operator ()(const
std::binder2nd<_Fn2>::argument_type &) const' with
[
_Fn2=std::mem_fun1_ref_t<void,Value,int>
]
see reference to class template instantiation 'std::binder2nd<_Fn2>'
being compiled with
[
_Fn2=std::mem_fun1_ref_t<void,Value,int>
]

Is this a valid error?
What can I do to fix the problem?


It is a valid error, but it is also a situation that the C++ standards
committee intends to address. Library issue 109,

You can find the issues list off of this page:

http://www.open-std.org/jtc1/sc22/wg21/

but as I write this, the site is down.

109 has WP status will cause your code to compile and run as expected.
This means that the committee has accepted it into the draft C++0X
standard. So technically, 109 isn't standard now, but has a good chance
of becoming so. The issue adds a non-const overload to std::binder2nd

typename Operation::result_type // experimental
operator()(typename Operation::first_argument_type& x) const
{return op(x, value);}

Apparently 2/3 of your vendors have already implemented this fix (fwiw,
Metrowerks has).

You could possibly patch your copy of std::binder2nd that hasn't yet
implemented 109.

Alternatively, you could use std::tr1::bind, or boost::bind, which I
personally find easier to use:

using namespace std::tr1::placeholders;

std::for_each(values.begin(), values.end(),
std::tr1::bind(&Value::ChangeTo, _1, 7));

std::tr1::bind is a "TR1" library that has been proposed for C++0X and
accpeted into the Library Technical Report 1. It is based on
boost::bind (www.boost.org). Your vendor may or may not yet supply
std::tr1::bind. If you use boost, then it would look like:

std::for_each(values.begin(), values.end(),
boost::bind(&Value::ChangeTo, _1, 7));

The bind function creates a function object using it's first parameter
as a function that is bound to the following parameters of bind. Those
parameters you want left unbound are designated by _1, _2, etc. When
the first parameter is a member function, then the first argument to the
resulting function object is always a pointer to (or smart pointer to,
or reference to) the object. In your example, this parameter is left
free, and the literal '7' is bound to the second parameter, resulting in
a functor that takes a single parameter: the Value&.

-Howard
Jul 22 '05 #2
Tron Thomas wrote:

Given the following code:

#include <vector>
#include <algorithm>
#include <functional>

class Value
{
public:
explicit Value(int value) : m_value(value) {}
void ChangeTo(int value) { m_value = value; }

private:
int m_value;
};

int main()
{
std::vector<Value> values;
values.insert(values.end(), 5, Value(6));

std::for_each(values.begin(), values.end(),
std::bind2nd(std::mem_fun_ref(&Value::ChangeTo), 7));

return 0;
}

Under two different compiler, the code compiles without any warnings
or errors. A third compiler produces an error with output like the
[...]
Is this a valid error?
Yes, but... the issue is in the Standard Library Defect Report List:
http://www.open-std.org/jtc1/sc22/wg...fects.html#109

Some popular implementations already address it by including an
as of yet non-standard binder2nd::operator() for non-const
arguments.
What can I do to fix the problem?


You can write your own binder if your library doesn't implement
the proposed resolution.

Denis
Jul 22 '05 #3
Thanks Howard and Denis for you replies. This has clear things up. I
solved the problem by just not using an binder and implementing my own
functor to pass into for_each.

I played around with the original code and the compilers (or STL
implementaions) that could successfully compile that code. I
discovered that for one implementation the values in the container
were modified, and for the other implementation the values were not
modified. Does this imply a bug in the implementation where the
values were not modified?
Jul 22 '05 #4

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

Similar topics

17
by: DanielESFA | last post by:
Hey guys :) This is a bit of a funny one... We're four guys working on the same project, everybody using KDevelop and g++ on Linux. Three of us are using Mandrake, with g++ 3.4.3 and 3.4.1....
6
by: Jason | last post by:
I have a function (Inet_ntop) that returns const char * and if I try to assign that return value to a char * variable, I get the gcc error message: warning: assignment discards qualifiers from...
12
by: Charlie Zender | last post by:
Hi, I am unable to compile a large body of code with extremely pedantic compile time checks activate, so that warnings cause errors. With GCC 3.3.1, I do this with gcc -std=c99 -pedantic...
7
by: Lucas Tam | last post by:
Hi all, Does anyone know of a GOOD example on parsing text with text qualifiers? I am hoping to parse text with variable length delimiters/qualifiers. Also, qualified text could run onto...
0
by: jmarr02s | last post by:
I wish to supply text qualifiers to the following date time stamp function, Expr1: FormatDate(Now()) in my query, using fField1: Chr(34) & & Chr(34)...How must I write this? Next, I export the...
45
by: Master Programmer | last post by:
- Constant language / system changes - Dropping of the VB language - Security configuation minefields - Loss of old code base - Time consuming to learn (then it changes every 5 minutes) - Slow...
4
by: Lycan. Mao.. | last post by:
Hello, I'm trying to write a function adapter object, but it fails with the above information. Can you help me. template <typename _Predicate> struct Unary_negate { typedef typename...
17
by: Pietro Cerutti | last post by:
i Group, to my understanding, defining a function parameter as "const" means that the function is not going to change it. Why does the compiler says "return discards qualifiers from pointer...
4
by: Andre | last post by:
Hi All, When I compile the following piece of code with gcc, I get 3 "warning: initialization discards qualifiers from pointer target type" messages which refer to the 3 lines marked in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.