What does this line of code mean?

Discussion in 'C++' started by Bo Yang, Jan 19, 2007.

  1. Bo Yang

    Bo Yang Guest

    Now, I am reading The C++ Standard Library,
    there is one line of code which I can't understand.

    template <class 0P1, class 0P2, class 0P3>
    inline compose_f_gx_hx_t<0Pl,0P2,0P3>
    compose_f_gx_hx (const 0P1& o1, const 0P2& o2, const 0P3& o3) {
    return compose_f_gx_hx_t<0Pl,0P2,0P3>(ol,o2,o3);
    }

    I know what these code intended to do, but I can't understand the
    syntax here!

    the line begin with inline keyword, why is it there?
    Is this syntax standard C++?
     
    Bo Yang, Jan 19, 2007
    #1
    1. Advertising

  2. Bo Yang

    Noah Roberts Guest

    Bo Yang wrote:
    > Now, I am reading The C++ Standard Library,
    > there is one line of code which I can't understand.
    >
    > template <class 0P1, class 0P2, class 0P3>
    > inline compose_f_gx_hx_t<0Pl,0P2,0P3>
    > compose_f_gx_hx (const 0P1& o1, const 0P2& o2, const 0P3& o3) {
    > return compose_f_gx_hx_t<0Pl,0P2,0P3>(ol,o2,o3);
    > }
    >
    > I know what these code intended to do, but I can't understand the
    > syntax here!
    >
    > the line begin with inline keyword, why is it there?
    > Is this syntax standard C++?


    Yes it is standard. It is there as a hint for the compiler to inline
    that function. The function is a nicer constructor for a templated
    class so that you can create one without having to specify the template
    parameters.
     
    Noah Roberts, Jan 19, 2007
    #2
    1. Advertising

  3. Bo Yang

    David O Guest

    Bo Yang wrote:
    > Now, I am reading The C++ Standard Library,
    > there is one line of code which I can't understand.
    >
    > template <class 0P1, class 0P2, class 0P3>
    > inline compose_f_gx_hx_t<0Pl,0P2,0P3>
    > compose_f_gx_hx (const 0P1& o1, const 0P2& o2, const 0P3& o3) {
    > return compose_f_gx_hx_t<0Pl,0P2,0P3>(ol,o2,o3);
    > }
    >
    > I know what these code intended to do, but I can't understand the
    > syntax here!
    >


    This declaration is comparable to:

    template < class T > // for any type T
    inline compose_type<T> // function return type
    compose( T & t ) { // function parameters
    return compose_type<T>( t ); // return from constructor
    }

    In other words compose<T>() simply constructs and returns an
    compose_type<T> object for any type T. The code is obscured by having
    three template parameters, OP1/2/3, intead of T, and having easily
    confused names for the function and its result type - though knowing
    that a "_t" conventionally implies a type makes it easier to scan.

    > the line begin with inline keyword, why is it there?


    It's the return type.

    > Is this syntax standard C++?


    Yes.

    Best Regards,

    David O.
     
    David O, Jan 19, 2007
    #3
  4. Bo Yang

    Ron Natalie Guest

    Noah Roberts wrote:

    >
    > Yes it is standard. It is there as a hint for the compiler to inline
    > that function. The function is a nicer constructor for a templated
    > class so that you can create one without having to specify the template
    > parameters.
    >

    It also allows that the function definition appear more than once (a
    sort of precursor for most implementations to inline things).
     
    Ron Natalie, Jan 19, 2007
    #4
  5. Bo Yang

    David O Guest

    Bo Yang wrote:
    <snip>
    > inline compose_f_gx_hx_t<0Pl,0P2,0P3>

    <snip>

    > I know what these code intended to do, but I can't understand the
    > syntax here!
    >
    > the line begin with inline keyword, why is it there?


    Funny - I thought you were asking why the line "inline
    compose_f_gx_hx_t<0Pl,0P2,0P3>" was there, rather than why the was
    "inline" there! The other replies give excellent reasons for the
    "inline" being present.

    Another incidental benefit from its presence is that it assists human
    scanning of complex template declarations telling the reader that
    there's a function declaration lurking in there starting at the
    "inline".

    Best Regards,

    David O.
     
    David O, Jan 19, 2007
    #5
  6. Bo Yang

    Bo Yang Guest

    David O :
    > Bo Yang wrote:
    > <snip>
    >> inline compose_f_gx_hx_t<0Pl,0P2,0P3>

    > <snip>
    >
    >> I know what these code intended to do, but I can't understand the
    >> syntax here!
    >>
    >> the line begin with inline keyword, why is it there?

    >
    > Funny - I thought you were asking why the line "inline
    > compose_f_gx_hx_t<0Pl,0P2,0P3>" was there, rather than why the was
    > "inline" there! The other replies give excellent reasons for the
    > "inline" being present.
    >
    > Another incidental benefit from its presence is that it assists human
    > scanning of complex template declarations telling the reader that
    > there's a function declaration lurking in there starting at the
    > "inline".
    >
    > Best Regards,
    >
    > David O.
    >


    Thank you, I forget the function return type is a object.
    Thank you!
     
    Bo Yang, Jan 20, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Harrison

    Re: What does this line mean

    John Harrison, Aug 6, 2003, in forum: C++
    Replies:
    2
    Views:
    335
    emerth
    Aug 7, 2003
  2. Valentina

    What does this line mean?

    Valentina, Oct 3, 2004, in forum: C++
    Replies:
    2
    Views:
    443
    Pedro Graca
    Oct 3, 2004
  3. Binary

    what does this line mean?

    Binary, Oct 24, 2006, in forum: C++
    Replies:
    6
    Views:
    303
    Alf P. Steinbach
    Oct 24, 2006
  4. Rahul
    Replies:
    4
    Views:
    603
    Robert Kern
    Apr 7, 2009
  5. C Barrington-Leigh
    Replies:
    1
    Views:
    1,232
    Tim Leslie
    Sep 10, 2010
Loading...

Share This Page