[CMake] functions as first class objects

Aaron_Wright at selinc.com Aaron_Wright at selinc.com
Thu Nov 19 11:08:47 EST 2009


I agree with Michael. C/C++ does not allow you pass around functions. You 
can only pass around pointers to functions.

Perhaps this line is confusing you:

int (*Func)(int,int) = SomeFunction;

This is not assigning SomeFunction to Func. It's assigning a pointer to 
SomeFunction to Func. It technically should be written as:

int (*Func)(int,int) = &SomeFunction;

But the compiler is helping you out, or hurting you depending on the way 
you look at it.

---------------------------------------------------------
Aaron Wright
Software Engineer - DCS Group
Schweitzer Engineering Laboratories, Inc.
Pullman, WA 99163
509-334-8087



Michael Wild <themiwi at gmail.com> 
Sent by: cmake-bounces at cmake.org
11/19/2009 07:46 AM

To
Jed Brown <jed at 59A2.org>
cc
CMake List <cmake at cmake.org>
Subject
Re: [CMake] functions as first class objects







On 19. Nov, 2009, at 15:44 , Jed Brown wrote:

> Michael Wild wrote:
>>
>> On 19. Nov, 2009, at 15:06 , Jed Brown wrote:
>>
>>> Michael Wild wrote:
>>>
>>>> Not sure I'd like that... Instead of being more expressive, I 
>>>> think this
>>>> would be very confusing.
>>>
>>> This is not a some magic beast coming out of functional 
>>> languages.  In
>>> fact, it's pretty hard to find a language that can't do this sort of
>>> thing, even older Fortrans let you pass functions.
>>>
>>> Jed
>>
>> Yes, but that's something completely different! It's not like you're
>> able to do (using your notation):
>
> (not my notation)
>
>> set(&tmp &install)
>> set(&install &add_executable)
>> set(&add_executable &tmp)
>>
>> In C/C++ and to various extent in Fortran (depending on the 
>> version) you
>> can pass around "function pointers". This is _not_ a function. 
>> There's
>> no way of doing that in C/C++ or Fortran (that is, without using ugly
>> preprocessor magic).
>
> CMake is untyped where as C, for example, is statically typed and only
> allows symbols to be defined once (C++ breaks this with templates, but
> you're supposed to be careful so that all definitions are equivalent).

Although this is OT, I have to say that this is not true. C++ is still 
statically typed. You can overload functions, but then it really is 
the same thing as the function signature is considered to be part of 
the name. Also templates do not change this, as the template 
parameters are part of the type name.

> In any case, you sure can do
>
>  int (*Func)(int,int) = SomeFunction;
>  int x = Func(5,6);
>  int y = FoldL(Func,array,size,3);
>
> and so on.
>

Sure, but you're not _renaming_ a function. You're just assigning the 
address of a function to a pointer which can then be used to invoke 
that function. It is _always_ clear that Func is a pointer to a 
function and not something else.

> Jed
>

Anyways, I didn't want to get into a language-war, but rather voiced 
my dislike for such dubious flexibility. CMake is very domain-specific 
language used to create build systems, not a general-purpose language.

BTW, in the OP's particular case I would prefer to patch the upstream 
source without any voodoo.

Michael

_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Follow this link to subscribe/unsubscribe:
http://www.cmake.org/mailman/listinfo/cmake

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20091119/0b63ffc0/attachment.htm>


More information about the CMake mailing list