Hi, I have read this on IBM:

An exception specification may only appear at the end of a function declarator of a function, pointer to function, reference to function, pointer to member function declaration, or pointer to member function definition. An exception specification cannot appear in a typedef declaration. The following declarations demonstrate this:

Can anyone write me an example of reference to function (so it contains exception specification with it)?

And another question about syntax I can't understand. What does this mean:

void h(void i() throw(int));

Thanks.

void h(void i() throw(int));

That is the declaration of a function called h which has no return value and takes one parameter, called i, which is a function with no return value and no parameters and that can only throw an int. When it comes to functions, you can have function types, function-pointer types, function-reference types, and all of those for member-functions as well. I know this can be confusing, even more so because these three types have implicit conversion rules between them (a function can implicitly convert to either a function-pointer or function-reference, and vice versa, and the same goes for member functions). There are few good explanations of this, and if you really want to understand it clearly, read the relevant sections of the C++ standard document, in particular (in draft n3337): 3.1, 3.9, 4.3, 8.3.5-6, 8.4, 13.4, and, of course, 15.4 (where the IBM page got its quote and examples from, and the C++ standard is much clearer than the IBM page on this). When you read the sections of the standard pertaining to function types and such, you eventually get an epiphany and realize how it all works.

Internally, it really doesn't matter much which type you have between a function type, a function-pointer type or function-reference type, they are probably all treated pretty much the same by the compiler.

Here are a few examples that might clear things up:

typedef int F(int);      // function type F.
typedef int (*pF)(int);  // function-pointer type pF.
typedef F* pF;           // this is equivalent to the above.
typedef int (&rF)(int);  // function-reference type rF.
typedef F& rF;           // this is equivalent to the above.

int f(int);  // declaration of function f.
F f;         // this declaration is equivalent to the above declaration.

// the function f can be defined as follows:
int f(int i) {
  return i + 42;
};

int main() {
  pF p_f1 = f;   //implicit conversion.
  pF p_f2 = &f;  //equivalent to the above.

  rF r_f1 = f;   //implicit conversion.

  return 0;
};

I didn't include the exception specifications in the above, simply because they are not allowed in a typedef. So, it means:

typedef int (*F)(int) throw(int); // ERROR, except-spec not allowed in typedef.

int h(int (*f)(int) throw(int));  // OK.

This is really a weird thing about exception specifications: they are not really part of the type system in C++ (e.g. like cv-qualifiers or other attributes of functions). But the standard does require some rules about matching the exception specifications of function types, meaning that some type system is required to enforce that. This shadow type system that exception specifications require is often not implemented in many of the main stream compilers, they simply implement the one, over-arching, type system, and ignore the rules about this except-spec shadow type system (more or less ignoring exception specifications during compilation, implementing only the run-time behavior required).

I also recommend that you look at this gotw page on exception specifications.

Thank you for big reply and book.

So basically when I do this:
typedef int (*pF)(int);
I create new type named pf which creates pointers to functions with int as a parameter and a return type?

It confuses me a bit since this...
typedef short SmallNumber;
...doesn't look very familiar with example for pf, because there is no such thing as
typedef "original" "user-defined"
Where "user-defined" gets attributes from "original", do you understand what is my spot of stumbling?

huh...

Thanks.

Edited 4 Years Ago by MrEARTHSHAcKER: left character

Where "user-defined" gets attributes from "original", do you understand what is my spot of stumbling?

You must look at it as typedef <declaration>;. If you see that int var; is the declaration of a variable var, and that int func(int i); is the declaration of a function func. So, typedef int var_type; is a definition of a type equivalent to int, and similarly, typedef int func_type(int); is a definition of a type equivalent to a function taking an int as parameter and returning an int.

I agree that the syntax of function (-pointer or -reference) types is a bit counter intuitive or confusing. That's just the way it is, and read the relevant parts of the standard like I suggested, it will be much clearer then.

Edited 4 Years Ago by mike_2000_17: missed something

This article has been dead for over six months. Start a new discussion instead.