Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pointer to templated function

Reply
Thread Tools

Pointer to templated function

 
 
Juha Nieminen
Guest
Posts: n/a
 
      04-17-2011
In a discussion in another forum about curried functions, I wondered
if this could be, technically speaking, considered currying in C++:

//-----------------------------------------------------------------
#include <iostream>

void function(int a, int b, int c)
{
std::cout << a << " " << b << " " << c << "\n";
}

template<int lastValue>
void curriedFunction(int a, int b)
{
function(a, b, lastValue);
}

int main()
{
void(*functionWith5)(int,int) = &curriedFunction<5>;

functionWith5(1, 2); // Is this a curried function call?
}
//-----------------------------------------------------------------

When I wrote that, I was actually kind of expecting it to not to work
(iow. to get some kind of syntax error). However, to a bit of my surprise,
it does work.

I had never before thought about how one could make a pointer to a
templated function instantiation. After all, you can't template a pointer.
I was a bit surprised that a regular bare-bones function pointer can be
made to point to a template function instantiation. I had a gut feeling
that you could possibly get an error because the internal type symbols for
a template function instantiation might be different to that of a "raw"
function. However, apparently not. (OTOH, thinking about it, why shouldn't
it work? How else are you supposed to take a pointer to a templated
function instantiation?)

Is this really legit, or is it just a fluke?
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      04-17-2011
Juha Nieminen wrote:

> In a discussion in another forum about curried functions, I wondered
> if this could be, technically speaking, considered currying in C++:


Hm, without the qualification "technically speaking", I would say: sure, why
not. But I don't have an opionion to offer on, whether the code below
satisfies some technical definition of currying. Surely, I would first think
about std::bind() as templates are limited: (a) the fixed value has to be
known at compile time and (b) cannot have an arbitrary type.

> //-----------------------------------------------------------------
> #include <iostream>
>
> void function(int a, int b, int c)
> {
> std::cout << a << " " << b << " " << c << "\n";
> }
>
> template<int lastValue>
> void curriedFunction(int a, int b)
> {
> function(a, b, lastValue);
> }
>
> int main()
> {
> void(*functionWith5)(int,int) = &curriedFunction<5>;
>
> functionWith5(1, 2); // Is this a curried function call?
> }
> //-----------------------------------------------------------------
>
> When I wrote that, I was actually kind of expecting it to not to work
> (iow. to get some kind of syntax error). However, to a bit of my surprise,
> it does work.
>
> I had never before thought about how one could make a pointer to a
> templated function instantiation. After all, you can't template a pointer.
> I was a bit surprised that a regular bare-bones function pointer can be
> made to point to a template function instantiation. I had a gut feeling
> that you could possibly get an error because the internal type symbols for
> a template function instantiation might be different to that of a "raw"
> function. However, apparently not. (OTOH, thinking about it, why shouldn't
> it work? How else are you supposed to take a pointer to a templated
> function instantiation?)
>
> Is this really legit, or is it just a fluke?


It is, as far as I know, legit. A prominent use case: one can that technique
to implement the custom deleter in shared_ptr<> or the clone function in
smart pointers with deep copy semantics. There, however, the template
parameter would be a type, not a value.


Best,

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      04-17-2011
* Juha Nieminen, on 17.04.2011 11:32:
> In a discussion in another forum about curried functions, I wondered
> if this could be, technically speaking, considered currying in C++:
>
> //-----------------------------------------------------------------
> #include<iostream>
>
> void function(int a, int b, int c)
> {
> std::cout<< a<< " "<< b<< " "<< c<< "\n";
> }
>
> template<int lastValue>
> void curriedFunction(int a, int b)
> {
> function(a, b, lastValue);
> }
>
> int main()
> {
> void(*functionWith5)(int,int) =&curriedFunction<5>;
>
> functionWith5(1, 2); // Is this a curried function call?
> }
> //-----------------------------------------------------------------
>
> When I wrote that, I was actually kind of expecting it to not to work
> (iow. to get some kind of syntax error). However, to a bit of my surprise,
> it does work.
>
> I had never before thought about how one could make a pointer to a
> templated function instantiation. After all, you can't template a pointer.
> I was a bit surprised that a regular bare-bones function pointer can be
> made to point to a template function instantiation.


The instantiation (or full specialization) is in effect an ordinary function.

In some cases that ordinariness can bite you.

For example, in a header file it's OK to define ...

template< class Type >
int foo() { return sizeof( Type ); } // Or whatever.

.... but a specialization like ...

template<>
int foo<float>() { return sizeof(float); }

.... is bad bad bad -- because of lacking keyword "inline", which is
practically needed for an ordinary function definition in a header file.


> I had a gut feeling
> that you could possibly get an error because the internal type symbols for
> a template function instantiation might be different to that of a "raw"
> function. However, apparently not. (OTOH, thinking about it, why shouldn't
> it work? How else are you supposed to take a pointer to a templated
> function instantiation?)
>
> Is this really legit, or is it just a fluke?


It's OK.

But I'm not sure about the terminology; I'd have to look up "currying"; mostly
it reminds me of certain heavily spiced Pakistani dinners (and just a little
functional programming and academic things)...

Also, it's pretty limited: the template argument needs to an integer-based type
or pointer.

Argh, more terminology; I can't recall what that is called.

But anyway, check out Boost 'bind' for more general functionality like that.


Cheers & hth.,

- Alf

--
blog at <url: http://alfps.wordpress.com>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Defining templated member function outside templated class chhenning C++ 5 02-13-2008 07:36 PM
templated function as parameter of another templated function Amadeus W. M. C++ 2 07-04-2006 09:59 PM
ASP.NET Templated User Controls - Limit child controls allowable within a templated control JohnyStyles@gmail.com ASP .Net 0 05-29-2006 06:00 PM
Subtypes of templated types (in templated functions) Marijn C++ 5 02-13-2004 09:50 AM
implementing a templated struct within a templated struct RA Scheltema C++ 3 01-06-2004 11:25 AM



Advertisments