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... 6 2215
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_argu ment_type,
typename Op::result_type > {
protected:
Op op_;
typename Op::first_argum ent_type val_;
public:
refbinder1st(co nst Op& op,
const typename Op::first_argum ent_type val) : op_(op),
val_(val) {}
typename Op::result_type operator ()(
const typename Op::second_argu ment_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(cons t Op& op, T v_1st) {
return refbinder1st<Op >(op, typename Op::first_argum ent_type(v_1st) );
}
Denis
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
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.
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.
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 void Attach( function<void (Subject*)> o ) {
obs_.push_back(o); } --Line1
|
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 overloaded assignment operator implemented as follows:
Point &Point::operator=( const Point *somePoint )
|
by: John Black |
last post by:
I have the following code trying to use bind1st,
class C1{
...
};
class C2{
...
};
|
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 <iostream>
#include <list>
#include <algorithm>
|
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 constness of static members. Is
there a way to do that? Also, is there a way for a static member
function to guarantee constness of static members?
TIA
Regards,
| |
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.
vector::iterator it2 =
std::find_if (v1.begin (), v1.end (),
std::bind1st (equal_to (), 3));
is better than
|
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 <boost/shared_ptr.hpp>
#include <vector>
#include <iterator>
#include <iostream>
class trial {
|
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 );
};
|
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 { const typename cv; typename v; }*)&(value))->v)
which requires value to be an lvalue, but avoid compiler warning (gcc)
comparing to the brute force of ((typename)(value)):
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |