Is std::tr1::function intended to work ONLY with function objects which implement the operator() as const? - c++
This is a discussion on Is std::tr1::function intended to work ONLY with function objects which implement the operator() as const? - c++ ; Hi,
I'm using an STL implementation that fails to compile the below code.
So the question is: is the below code standard? Or the behavior is
implementation-specific?
------------------------------------------------------------------------------
#include <functional>
struct functor
{
void operator() (int) { } // not ...
-
-
Re: Is std::tr1::function intended to work ONLY with function objectswhich implement the operator() as const?
Florin Neamtu ha scritto:
> Hi,
>
> I'm using an STL implementation that fails to compile the below code.
Could you please post which compiler and the exact error message?
> So the question is: is the below code standard? Or the behavior is
> implementation-specific?
AFAIK, the code is supposed to be ok. In this case there is no need to
have an explicit statement allowing such possibility: in absence of an
explicit statement against it, we can safely assume it's ok.
BTW in the docs of Boost.Function there is an indirect assumption that
it's possible in section "References to Function Objects" here:
http://boost.org/doc/html/function/t...html#id1187118
HTH,
Ganesh
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
-
Re: Is std::tr1::function intended to work ONLY with function objects which implement the operator() as const?
On May 18, 8:02 pm, Florin Neamtu <nea...@zappmobile.ro> wrote:
> Hi,
>
> I'm using an STL implementation that fails to compile the below code.
> So the question is: is the below code standard? Or the behavior is
> implementation-specific?
>
> ------------------------------------------------------------------------------
> #include <functional>
>
> struct functor
> {
> void operator() (int) { } // not const
>
> };
>
> int main()
> {
> functor fun;
> std::tr1::function<void(int)> f = fun; // Is this standard?
> return 0;}
>
> ------------------------------------------------------------------------------
>
> My understanding is that the code is standard compliant.
I believe that you are right and that the code is TR1-compliant. The
requirement of the constructor is that its argument "shall be
callable", and 'fun' fits the definition of Callable. In addition, the
specification of function<>:
perator() does not say that the target
is invoked as const, so f(5) should work as well, although this isn't
100% clear from the standard text (even though it's 96% clear in my
opinion).
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
-
Re: Is std::tr1::function intended to work ONLY with function objects which implement the operator() as const?
On 18 May, 18:41, AlbertoBarb...@libero.it (Alberto Ganesh Barbati)
wrote:
> Florin Neamtu ha scritto:
>
> > Hi,
>
> > I'm using an STL implementation that fails to compile the below code.
>
> Could you please post which compiler and the exact error message?
>
> > So the question is: is the below code standard? Or the behavior is
> > implementation-specific?
>
> AFAIK, the code is supposed to be ok. In this case there is no need to
> have an explicit statement allowing such possibility: in absence of an
> explicit statement against it, we can safely assume it's ok.
>
> BTW in the docs of Boost.Function there is an indirect assumption that
> it's possible in section "References to Function Objects" here:http://boost.org/doc/html/function/t...html#id1187118
>
> HTH,
>
> Ganesh
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-...@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ:http://www.comeaucomputing.com/csc/faq.html ]
The error is related to the implementation of STL that I currently use
and it doesn't make much sense to this topic. However this is the
header of the error returned by MS Visual C++ 2005 Pro:
error C3848: expression having type 'const functor' would lose some
const-volatile qualifiers in order to call 'void functor:
perator ()
(int)'
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
-
Re: Is std::tr1::function intended to work ONLY with function objects which implement the operator() as const?
On 20 May, 05:01, Peter Dimov <pdi...> wrote:
> On May 18, 8:02 pm, Florin Neamtu <nea...@zappmobile.ro> wrote:
>
>
>
>
>
> > Hi,
>
> > I'm using an STL implementation that fails to compile the below code.
> > So the question is: is the below code standard? Or the behavior is
> > implementation-specific?
>
> > ------------------------------------------------------------------------------
> > #include <functional>
>
> > struct functor
> > {
> > void operator() (int) { } // not const
>
> > };
>
> > int main()
> > {
> > functor fun;
> > std::tr1::function<void(int)> f = fun; // Is this standard?
> > return 0;}
>
> > ------------------------------------------------------------------------------
>
> > My understanding is that the code is standard compliant.
>
> I believe that you are right and that the code is TR1-compliant. The
> requirement of the constructor is that its argument "shall be
> callable", and 'fun' fits the definition of Callable. In addition, the
> specification of function<>:
perator() does not say that the target
> is invoked as const, so f(5) should work as well, although this isn't
> 100% clear from the standard text (even though it's 96% clear in my
> opinion).
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-...@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ:http://www.comeaucomputing.com/csc/faq.html ]- Hide quoted text -
>
> - Show quoted text -
Thanks for the discussion, it seems clear that it's reasonable to have
std::tr1::function instances calling the non-const operator() of the
aggregated functors. Just same as calling non-const member functions.
In fact, the job of std::tr1::function is to forward a call, not at
all to impose restrictions on the target callable object. At least
this was the purpose of such component and it is oulined so in TR1
draft too. I also don't find any technical difficulties or non-
portable issues in implementing it so.
Cheers!
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Similar Threads
-
By Application Development in forum Python
Replies: 5
Last Post: 10-23-2007, 05:45 AM
-
By Application Development in forum CSharp
Replies: 6
Last Post: 08-12-2007, 04:38 PM
-
By Application Development in forum c++
Replies: 8
Last Post: 07-26-2007, 06:52 AM
-
By Application Development in forum c++
Replies: 14
Last Post: 06-16-2007, 02:55 PM
-
By Application Development in forum c++
Replies: 0
Last Post: 03-19-2007, 12:53 PM