473,804 Members | 2,132 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

metaprogramming solution to recursive function typedef ?

typedef int foo ( foo ); // foo is a pointer-to-function type that takes
another foo as argument and returns an int

I need to achieve the above effect somehow. This is not accepted by any
compiler I have tried, even though I cant spot anything in the standard that
restricts it.

Is there a way to achieve this typedefintion using metaprogramming ? I
noticed that boost::variant does some metaprogramming tricks to allow

variants to be contained in variants. But it is unclear to me how that is
acheived, also that problem _may_ not be exactly the same as this.

-Roshan Naik


Oct 19 '05 #1
21 3867
Roshan Naik wrote:
typedef int foo ( foo ); // foo is a pointer-to-function type that takes
another foo as argument and returns an int

I need to achieve the above effect somehow.


What is the real underlying problem that you are trying to solve?

[snip]
Best

Kai-Uwe Bux
Oct 19 '05 #2
* Roshan Naik:
typedef int foo ( foo ); // foo is a pointer-to-function type that takes
another foo as argument and returns an int

I need to achieve the above effect somehow.


struct Foo
{
virtual int operator()( Foo const* ) const = 0;
};

Then if you need to pass such functors by value, you can wrap them in a
functor with a smart-pointer to the actual functor.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 19 '05 #3
Roshan Naik wrote:
typedef int foo ( foo ); // foo is a pointer-to-function type that takes
another foo as argument and returns an int

I need to achieve the above effect somehow. This is not accepted by any
compiler I have tried, even though I cant spot anything in the standard that
restricts it.

Is there a way to achieve this typedefintion using metaprogramming ? I
noticed that boost::variant does some metaprogramming tricks to allow

variants to be contained in variants. But it is unclear to me how that is
acheived, also that problem _may_ not be exactly the same as this.


Perhaps you want something along these lines:

template <class T>
struct CallFunctionPtr
{
typedef int (*functionPtr)( T);

typedef CallFunctionPtr <fPtr> next;
};

int main()
{
CallFunctionPtr <int(*)()>::nex t::functionPtr fPtr;

// fPtr is of type int (*)(int (*)())

CallFunctionPtr <int(*)()>::nex t::next:::funct ionPtr f2Ptr;

// f2Ptr is of type int (*)(int (*)(int (*)()))
}

Each "next" inner type corresponds to a function call to a function
whose pointer is being passed as a parameter. The number of "next"'s
that are chained together equals how many functions will be called
before the nested function pointers are exhausted.

Greg

Oct 19 '05 #4
Roshan Naik wrote:
typedef int foo ( foo ); // foo is a pointer-to-function type that takes
another foo as argument and returns an int


The comment is incorrect, for two reasons. First, foo is not defined,
because this is not a valid declaration. But you knew that. <g>

Second, if the argument type were changed from foo to, say, int, then
foo would not be a pointer to function type. It would be a function type.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Oct 19 '05 #5

"Alf P. Steinbach" <al***@start.no > wrote in message
news:43******** ********@news.i ndividual.net.. .
struct Foo
{
virtual int operator()( Foo const* ) const = 0;
};

Then if you need to pass such functors by value, you can wrap them in a
functor with a smart-pointer to the actual functor.


Thanks. But I already considered that and i need to have a real function
type. So i can use more
terse syntax like this...

int myFunc1 ( foo arg) {
//. ..
}

instead of having to write classes deriving from Foo and then implementing
( ) as a member function.

-Roshan
Oct 19 '05 #6

Roshan Naik wrote:
typedef int foo ( foo ); // foo is a pointer-to-function type that takes
another foo as argument and returns an int

<snip>

so foo is
a pointer to a function that has as a sole argument
a pointer to a function that has as a sole argument
a pointer to a function that has as a sole argument
.....

/dan

Oct 19 '05 #7

"Pete Becker" <pe********@acm .org> wrote in message
news:Rc******** ************@rc n.net...
Roshan Naik wrote:
typedef int foo ( foo ); // foo is a pointer-to-function type that takes another foo as argument and returns an int

The comment is incorrect, for two reasons. First, foo is not defined,
because this is not a valid declaration. But you knew that. <g>


Please elaborate. I assume that the above declaration (if it would be
accpeted by any compiler)
would be equivalent to.....
typedef int (*foo) (foo);

Note that I can use A* within the declaration of class A. Thus foo here
doesnt have to be defined as its a pointer type (my way of thinking).
Second, if the argument type were changed from foo to, say, int, then
foo would not be a pointer to function type. It would be a function type.


Is there such a thing as "function type" for variables in C++ ? It is my
understanding that you can only
have a pointers/references to functions. There is no way to define a
value-type variable that holds a function.
You can only declare a pointer-type variable that stores the address of a
function.

A function itself has a "function type" but from the standpoint of declaring
a variable/parameter
we have to use pointer to function types.

-Roshan
Oct 19 '05 #8
Roshan Naik wrote:

A function itself has a "function type" but from the standpoint of declaring
a variable/parameter
we have to use pointer to function types.


In C that is correct. In C++ you can also create a reference to function
type, although that's of minor utility. Nevertheless, in both C and C++,
typedef int foo(int); defines a function type, not a pointer to function
type. To define a pointer to function type you need a *, which says
'pointer'.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Oct 19 '05 #9
> In C that is correct. In C++ you can also create a reference to function
type, although that's of minor utility. Nevertheless, in both C and C++,
typedef int foo(int); defines a function type, not a pointer to function
type. To define a pointer to function type you need a *, which says
'pointer'.


And of what use is a function type ?

-Roshan
Oct 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1257
by: Dave | last post by:
For those who might be so inclined, I was wondering if I might get honest critiques of my first real venture into template metaprogramming. This template metaprogram sorts a list of integers at compile time. It works, but if my approach is more awkward or "bad" than need be, I'd love to hear suggestions for improvement! #include <iostream>
12
2165
by: Dave | last post by:
Would people agree with the statement that to a large degree, using template metaprogramming techniques turns a C++ compiler into a C++ interpreter (but just for the metaprogrammed portions of the code)? It's not a perfect analogy, but it seems to be a reasonable statement...
1
2360
by: mathieu | last post by:
Hello there, I am playing around with template metaprograming: I am trying to redefines my own types. But I am facing a small issue, where I cannot describe the whole implementation in one single class. Instead I have to separate implementation for binary and ascii in two different classes as I don't know how to use the traits technique for constructors. Any pointers very welcome !
8
1919
by: birchb | last post by:
While working on type expressions I am rather stuck for a way to express recursive types. A simple example of this is a singly-linked list of integers. In some languages you have compiler syntax which suspends evaluation so you can have recursive types. e.g. typedef Linked_List := int, Linked_List In LISP I would use a macro.
5
3371
by: Mark Stijnman | last post by:
I am trying to teach myself template metaprogramming and I have been trying to create lists of related types. I am however stuck when I want to make a template that gives me the last type in a list. I started by using a linked list of types with templates like: struct MyClass1 {}; struct MyClass2 {}; struct MyClass3 {}; struct NullType {};
9
3425
by: PengYu.UT | last post by:
Hi, I have the code below this email. I want to replace the last 4 lines with a Metaprogramming loop to get something like the following (I don't know the syntax). Is it possible? for type in {left_tag, right_tag, down_tag, up_tag) { fun(type()); }
4
1907
by: Alan Woodland | last post by:
I've been trying out more template metaprogramming ideas with typelists (mostly for personal learning, I'm aware boost most probably provides this facility already), and I've run into this small problem here. Comeau online (without C++0x, in strict mode) accepts this example pasted here, (apologies for the length of it). Unfortunately G++ (3.4, 4.1, 4.2) doesn't, and complains about index_find being private. The exact error it gives is:...
3
1856
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey Gurtovoy. I’m not doing this because I want to be a Meta Programming guru (because a lot of that stuff looks too crazy for use in the real world). Rather I want to learn heavyweight templates and this is the only
12
3375
by: nooneinparticular314159 | last post by:
Hello. If I declare the following: template<int a, int b, int SomeArray> class DoSomething{ public: .. .. ..
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10099
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3003
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.