views:

230

answers:

6
typedef void int_void(int);

int_void is a function taking an integer and returning nothing.

My question is: can it be used "alone", without a pointer? That is, is it possible to use it as simply int_void and not int_void*?

typedef void int_void(int);
int_void test;

This code compiles. But can test be somehow used or assigned to something (without a cast)?


/* Even this does not work (error: assignment of function) */
typedef void int_void(int);
int_void test, test2;
test = test2;
+1  A: 

I think it's legal - the following demonstrates its use:

typedef void f(int);

void t( int a ) {
}

int main() {
    f * p = t;
    p(1); // call t(1)
}

and actually, this C++ code compiles (with g++) & runs - I'm really not sure how kosher it is though.

#include <stdio.h>

typedef void f(int);

void t( int a ) {
    printf( "val is %d\n", a );
}

int main() {
    f & p = t;   // note reference not pointer
    p(1);
}
anon
Yes, I'm aware that you can use it as a pointer, but I specifically asked if it can be used without one :P (And, if not, why I can define a variable of that type) `My question is: can it be used "alone", without a pointer? That is, is it possible to use it as simply int_void and not int_void*?`
Andreas Bonini
@Andreas No. What would it mean?
anon
It is definitely kosher. Shorter form of `void(`
Johannes Schaub - litb
@Johannes But how come I can initialise a reference with a pointer?
anon
@Neil, it's not a pointer but it's a function. The function-to-pointer conversion is done only when needed, like with arrays. As written, `t` is an lvalue and has type `void(int)`.
Johannes Schaub - litb
@Neil, see http://stackoverflow.com/questions/1516958/could-someone-please-explain-the-difference-between-a-reference-and-a-pointer/1517195#1517195
Johannes Schaub - litb
@Johannes Thanks. Always good to learn something new - I don't think I've ever seen a function reference before, or maybe I just didn't notice them.
anon
Another question about it: http://stackoverflow.com/questions/480248/function-references
Johannes Schaub - litb
+5  A: 

What happens is that you get a shorter declaration for functions.

You can call test, but you will need an actual test() function.

You cannot assign anything to test because it is a label, essentially a constant value.

You can also use int_void to define a function pointer as Neil shows.


Example

typedef void int_void(int);

int main()
{
    int_void test; /* Forward declaration of test, equivalent to:
                    * void test(int); */
    test(5);
}

void test(int abc)
{
}
jbcreix
Thanks! That's a really cool way to confuse people! I added the testcase I wrote to make sure what you said actually worked, I hope you don't mind
Andreas Bonini
No, thank you. Alas, I can't figure a way to use it for the function itself, though. It would be cool to be able to eg. `pthread_entryfn a {`
jbcreix
It should be pointed out that the typedef can be used to declare a function, but it cannot be used to define the function, which is why in the example above the declaration of `test` and the definition look nothing alike. As far as I know, all other typedef'ed names can be used for both declaration and definition.
Michael Burr
@Andreas, not only it can confuse people, but also GCC: try `struct foo { void f(); }; typedef void ftype(); struct bar { friend ftype foo::f; };` and see GCC failing.
Johannes Schaub - litb
@Johannes: +1 even though if we had a +1 for every gcc bug we'd all be moderator gods.. ;-)
R..
In particular, you can use it for a function template: `template<typename T> int_void test;`
Johannes Schaub - litb
+1  A: 

Pointers to functions are values in C/C++. Functions are not.

Justice
A: 

It can be used in the following cases (out of the top of my head):

  • generic code:

    boost::function<int_void> func;

  • other typedefs:

    typedef int_void* int_void_ptr;

  • declarations:

    void add_callback(int_void* callback);

There may be others.

utnapistim
A: 

This should work, no casting required:

void f(int x) { printf("%d\n", x); }

int main(int argc, const char ** argv)
{
    typedef void (*int_void)(int);
    int_void test = f;
    ...
 }

A function's name "devolves" into a function pointer anytime you use the function's name in something other than a function call. If is is being assigned to a func ptr of the same type, you don't need a cast.

The original

typedef int_void(int);

is not useful by itself, without using a pointer to the type. So the answer to your question is "no, you can't use that typedef without a pointer".

Tim Schaeffer
+2  A: 

You are not declaring a variable; you are making a forward declaration of a function.

typedef void int_void(int);
int_void test;

is equivalent to

void test(int);
Jim Buck