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

reference to reference and constness problems with bind1st

T x;
T foo(T, T);
bind1st(ptr_fun(foo), x)

creates a function object that takes an argument of type T const&. This
does not work if T is already a reference type like int const&. So my
first problem is with the &. My second problem is with the const. Why
should bind1st change the constness of the second argument of the
function*?

I am currently using boost::bind instead, but I would rather not depend
on boost for all my programs...
Jul 22 '05 #1
6 2192
Marc wrote:

T x;
T foo(T, T);
bind1st(ptr_fun(foo), x)

creates a function object that takes an argument of type T const&. This
does not work if T is already a reference type like int const&. So my
first problem is with the &. My second problem is with the const. Why
should bind1st change the constness of the second argument of the
function ?

If instead of bind1st(Op const& op, T const& y) we had
bind1st(Op const& op, T& y), it would cause a problem deducing a
const reference template argument when used with, e.g. a temporary
or a numeric literal:
bind1st(ptr_fun(foo), 13);

The deduced type of template parameter T would be int, not const int,
T& would not bind to the argument, and the above would fail to compile.

I am currently using boost::bind instead, but I would rather not depend
on boost for all my programs...


You could change the parameters of foo from references to pointers.

When I was experimenting with this problem (and couldn't use boost)
I came up with a rather uncouth alternative of duplicating the standard
bind1st for the case when the template parameters are references (I've
never used it for real though):

template <class Op>
class refbinder1st : public unary_function<typename Op::second_argument_type,
typename Op::result_type> {
protected:
Op op_;
typename Op::first_argument_type val_;
public:
refbinder1st(const Op& op,
const typename Op::first_argument_type val) : op_(op),
val_(val) {}
typename Op::result_type operator ()(
const typename Op::second_argument_type arg) const {
return op_(val_, arg);
}
};

// Both arguments of Op should be references or very small objects
template <class Op, typename T>
refbinder1st<Op> refbind1st(const Op& op, T v_1st) {
return refbinder1st<Op>(op, typename Op::first_argument_type(v_1st));
}
Denis
Jul 22 '05 #2
On Mon, 5 Jul 2004 18:21:50 +0000 (UTC), Marc <Ma***********@Loria.Fr>
wrote:
T x;
T foo(T, T);
bind1st(ptr_fun(foo), x)

creates a function object that takes an argument of type T const&. This
does not work if T is already a reference type like int const&. So my
first problem is with the &. My second problem is with the const. Why
should bind1st change the constness of the second argument of the
function*?
Just the way it is written. Perfect argument passing is an unsolved
problem of C++, although the next version of the standard is going to
address it I think. In the absense of perfect argument passing, old
code used const references. Some new code uses more sophisticated
schemes, such as the call_traits in boost, or even rvalue/lvalue/const
resolving stuff (e.g. Mojo).

I am currently using boost::bind instead, but I would rather not depend
on boost for all my programs...


Boost contains too much essential stuff not to depend on it. I'd just
end up rewriting half of it if I couldn't use it. However, you can
just lift out the <boost/functional.hpp> header for a replacement
<functional> header that works much better.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #3
Denis Remezov wrote:
If instead of bind1st(Op const& op, T const& y) we had
bind1st(Op const& op, T& y), it would cause a problem deducing a
const reference template argument when used with, e.g. a temporary
or a numeric literal:
bind1st(ptr_fun(foo), 13);

The deduced type of template parameter T would be int, not const int,
T& would not bind to the argument, and the above would fail to compile.
I thought that it could be overloaded with a const and a non-const
versions. But I don't have time to check.
When I was experimenting with this problem (and couldn't use boost)
I came up with a rather uncouth alternative of duplicating the standard
bind1st for the case when the template parameters are references (I've
never used it for real though):


That is interesting, and it seems to work fine. Thank you for your
answer.

Jul 22 '05 #4
tom_usenet wrote:
Just the way it is written. Perfect argument passing is an unsolved
problem of C++, although the next version of the standard is going to
address it I think.
Do you have a reference on this "perfect argument passing" issue*?
Boost contains too much essential stuff not to depend on it. I'd just
end up rewriting half of it if I couldn't use it. However, you can
just lift out the <boost/functional.hpp> header for a replacement
<functional> header that works much better.


Cool, I had not seen this in boost. I hope the next standard benefits
from it.

Thank you for your answer.
Jul 22 '05 #5
On Tue, 6 Jul 2004 13:00:43 +0000 (UTC), Marc <Ma***********@Loria.Fr>
wrote:
tom_usenet wrote:
Just the way it is written. Perfect argument passing is an unsolved
problem of C++, although the next version of the standard is going to
address it I think.


Do you have a reference on this "perfect argument passing" issue*?


http://www.open-std.org/jtc1/sc22/wg...2002/n1385.htm
Boost contains too much essential stuff not to depend on it. I'd just
end up rewriting half of it if I couldn't use it. However, you can
just lift out the <boost/functional.hpp> header for a replacement
<functional> header that works much better.


Cool, I had not seen this in boost. I hope the next standard benefits
from it.


Unlikely I think, but boost::bind is part of the standard library
technical report (so it will be std::tr1::bind, and should come with
your compiler):
http://www.open-std.org/jtc1/sc22/wg...al_report.html
It's due in the next year.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #6
Marc wrote:

Denis Remezov wrote:
If instead of bind1st(Op const& op, T const& y) we had
bind1st(Op const& op, T& y), it would cause a problem deducing a
const reference template argument when used with, e.g. a temporary
or a numeric literal:
bind1st(ptr_fun(foo), 13);

The deduced type of template parameter T would be int, not const int,
T& would not bind to the argument, and the above would fail to compile.


I thought that it could be overloaded with a const and a non-const
versions. But I don't have time to check.


Yes, but the problem is the automatic template parameter deduction:

template <typename T>
void f(T& v) {
}

int main() {
const int a=13;
f(a); //all right

f<int const>(13); //all right as well

f(13); //error
}

Denis
Jul 22 '05 #7

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

Similar topics

1
by: wenmang | last post by:
Hi, all: I am reading Herb Sutter's article: http://www.cuj.com/documents/s=8840/cujexp0309sutter/ I have trouble to understand for following lines: class Subject { // ... public: virtual...
15
by: Trevor Lango | last post by:
I want to be able to cast away the constness of a private member variable in a member function of a class. I have the private data member declared as follows: const double x; I have an...
3
by: John Black | last post by:
I have the following code trying to use bind1st, class C1{ ... }; class C2{ ... };
2
by: Alberto | last post by:
Hello, while writing a program I ran across the problem of using for_each. Although I can traverse lists with a for loop, I'd prefer to use STL's for_each. Here's my faulty code: #include...
8
by: Srini | last post by:
Hello all, I was just wondering about this. A const member function guarantees constness of the object within the function body. But there's no way for a member function to guarantee the...
5
by: silverburgh.meryl | last post by:
I am reading an bind1st example from http://www.roguewave.com/support/docs/sourcepro/stdlibref/bind1st.html Can someone please tell me why // Even better, construct the new predicate on the fly....
14
by: PengYu.UT | last post by:
In the following program, I want an iterator contain pointer pointing to constant object not const pointer. If it is possible would you please let me know how to do it? #include...
3
by: Chris Roth | last post by:
I have a vector (v) containing objects of class C. class C { private: double d; public: void foo( B& b ); };
2
by: Laurent Deniau | last post by:
I am looking for the "cleanest" way to cast away the constness of a pointee in C89, something like const_cast<T*>() in C++. Actually, I am using: #define CONST_CAST(typename,value) \ (((union {...
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: 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
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
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?
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
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
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...

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.