Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   What does this line of code mean? (http://www.velocityreviews.com/forums/t459932-what-does-this-line-of-code-mean.html)

Bo Yang 01-19-2007 03:22 PM

What does this line of code mean?
 
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++?

Noah Roberts 01-19-2007 03:42 PM

Re: What does this line of code mean?
 

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.


David O 01-19-2007 03:43 PM

Re: What does this line of code mean?
 
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.


Ron Natalie 01-19-2007 03:53 PM

Re: What does this line of code mean?
 
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).


David O 01-19-2007 05:26 PM

Re: What does this line of code mean?
 
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.


Bo Yang 01-20-2007 01:09 AM

Re: What does this line of code mean?
 
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!


All times are GMT. The time now is 02:17 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.