public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* std::tr1::function wierdness
@ 2006-07-10 16:02 Adrian Harris
  2006-07-10 18:14 ` Christoph Bartoschek
  0 siblings, 1 reply; 2+ messages in thread
From: Adrian Harris @ 2006-07-10 16:02 UTC (permalink / raw)
  To: gcc-help


I'm playing around with std::tr1 in gcc 4.1.0 using a piece of code borrowed 
from 'Effective C++ Third Ed' and I cannot figure out the two different error 
messages.

Undoubtedly there is some subtle issue at work, but it escapes me.

Thanks

Code lifted and modified from pp 173..175:
-------
#include <tr1/functional>

class B;
int defaultFn( const B & ) { return 0; }

struct DefaultFnObj
{
     int operator()( const B & ) const { return 0; }
};

class B {
  public:
     typedef std::tr1::function<int ( const B & )> Fn_t;

     explicit B( Fn_t fn = defaultFn ) :
         m_fn( fn ) {}
     int fn() const { return m_fn( *this ); }
  private:
     Fn_t m_fn;
};

int main()
{
     // This works
     DefaultFnObj fn0;
     B b0( fn0 );
     const int rc0 = b0.fn();

     // This doesn't because of the explicit constructor call
     DefaultFnObj fn1();
     B b1( fn1 );
     const int rc1 = b1.fn();

     // This doesn't either (similar to above, but different compiler error)
     B b2( DefaultFnObj() );

     return rc0 + rc1 + b2.fn();
}
------


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: std::tr1::function wierdness
  2006-07-10 16:02 std::tr1::function wierdness Adrian Harris
@ 2006-07-10 18:14 ` Christoph Bartoschek
  0 siblings, 0 replies; 2+ messages in thread
From: Christoph Bartoschek @ 2006-07-10 18:14 UTC (permalink / raw)
  To: gcc-help

Am Montag, 10. Juli 2006 17:59 schrieb Adrian Harris:

> int main()
> {
...
>      B b2( DefaultFnObj() ); // this line compiles (1)
>
>      return rc0 + rc1 + b2.fn(); //this line has the error (2)
> }


b2 is not an object here. It is an function declaration. You have a function 
declared that takes a DefaultFnObj and returns a B. To get rid of the error:

B b2( (DefaultFnObj()) );

See Item 6:

http://www.accu.org/mdevelopers/projects/effectivecpp/estl_items.html

Christoph

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2006-07-10 18:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-07-10 16:02 std::tr1::function wierdness Adrian Harris
2006-07-10 18:14 ` Christoph Bartoschek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).