cyclic dependency in throw declaration

S

sgurukrupa

I am trying to solve a weird problem because of the following design
that I want to implement.

I have an 'exception' class that no other function must be able to
throw except a select group of functions. I want the compiler to
prohibit people who use my code the capability of throwing instances
of my exception class. However they will still be able catch objects
of the exception class. Why do I want to do this ? This brings clarity
to the working of the code - the source of all instances of my
exception class is only one - my code. I could have easily acheived
this in Java by making the constructor of my exception class package-
private ( which I acheive by not specifying any access modifier for
the constructor ) . I would then put the exception class inside the
package where my code will be making new instances of the exception
class. But C++ lacks this kind of access modifier. Now how should I
design my exception class to achieve this in C++ ?

I resorted to making the constructor of my exception class as private
and making all the functions of my code which will be allowed to throw
instances of this exception class, as 'friend's of the class. A
representative piece of code of this design is given here:

struct m
{
private:
m ( )
{ }

friend void myFunc ( ) throw ( m ) ;
} ;

void myFunc ( ) throw ( m )
{
}

int main ( )
{
return 0 ;
}

The problem is that the above code doesn't compile in g++ ( GNU
version of c++ compiler ). It throws the following errors:

structure.cpp:8: error: invalid use of undefined type ‘struct m’
structure.cpp:3: error: forward declaration of ‘struct m’

Even more surprising fact is that the above code won't compile even if
I change the line 'friend void myFunc ( ) throw ( m )' to :
friend void myFunc ( ) throw ( m * )
and also make the corresponding change in the function definition of
'myFunc' !

However, it does compile only when I change this line to :
friend void myFunc ( ) throw ( m * * )
Here's the complete code:

struct m
{
private:
m ( )
{ }

friend void myFunc ( ) throw ( m * * ) ;
} ;

void myFunc ( ) throw ( m * * )
{
}

int main ( )
{
return 0 ;
}

Why does g++ allow me throw a pointer to a pointer of type 'struct m'
but not a pointer of type 'struct m' ?
 
N

Neelesh

I am trying to solve a weird problem because of the following design
that I want to implement.

I have an 'exception' class that no other function must be able to
throw except a select group of functions. I want the compiler to
prohibit people who use my code the capability of throwing instances
of my exception class. However they will still be able catch objects
of the exception class. Why do I want to do this ? This brings clarity
to the working of the code - the source of all instances of my
exception class is only one - my code. I could have easily acheived
this in Java by making the constructor of my exception class package-
private ( which I acheive by not specifying any access modifier for
the constructor ) . I would then put the exception class inside the
package where my code will be making new instances of the exception
class. But C++ lacks this kind of access modifier. Now how should I
design my exception class to achieve this in C++ ?
One option is to derive the class m from a base class, and use the
base class in the exception specification:

struct base
{
virtual ~base();
};

base::~base() { }


struct m : public base
{
private:
m() { }
m(const m&) { } //Note that copy constructor is also private
friend void myFunc () throw (base) ;
} ;


Now the function myFunc can throw an exception of type m:

void myFunc () throw (base)
{
m m;
throw m; //Works
}


but no other function can, even if it gets a "readymade" reference to
m:

void otherFunc(const m& m) throw (base)
{
throw m; //Gives Error, because Copy Constructor of m is private.
This will work if the CC is public.
}


What is the drawback of this? You can no more catch by value. You can,
however, still catch by reference:

int main ( )
{
try
{
myFunc();
}
catch(m) //ERROR, catch by value not allowed, use m& instead.
{
}

}


Finally, answer to your last question:
I resorted to making the constructor of my exception class as private
and making all the functions of my code which will be allowed to throw
instances of this exception class, as 'friend's of the class. A
representative piece of code of this design is given here:

struct m
{
        private:
                m ( )
                { }

        friend void myFunc ( ) throw ( m ) ;

} ;

void myFunc ( ) throw ( m )
{

}

int main ( )
{
        return 0 ;

}

The problem is that the above code doesn't compile in g++ ( GNU
version of c++ compiler ). It throws the following errors:

structure.cpp:8: error: invalid use of undefined type ‘struct m’
structure.cpp:3: error: forward declaration of ‘struct m’

Even more surprising fact is that the above code won't compile even if
I change the line 'friend void myFunc ( ) throw ( m )' to :
friend void myFunc ( ) throw ( m * )
and also make the corresponding change in the function definition of
'myFunc' !

However, it does compile only when I change this line to :
friend void myFunc ( ) throw ( m * * )
Here's the complete code:

struct m
{
        private:
                m ( )
                { }

        friend void myFunc ( ) throw ( m * * ) ;

} ;

void myFunc ( ) throw ( m * * )
{

}

int main ( )
{
        return 0 ;

}

Why does g++ allow me throw a pointer to a pointer of type 'struct m'
but not a pointer of type 'struct m' ?

Because the c++ standard says so. 15.4p1 says:
A type denoted in an exception specification shall not denote a
pointer or reference to an incomplete type, other than void*, const
void*, volatile void*, or const volatile void*

In the current example, m is an incomplete type when it is first
encountered in the declaration of myFunc inside the class m. Hence m
or m* are not allowed. m** is neither an incomplete type, nor it is a
pointer to incomplete type. Hence m** is allowed.
 
N

Neelesh

One option is to derive the class m from a base class, and use the
base class in the exception specification:

struct base
{
  virtual ~base();

};

base::~base() { }

Well I think it is also a good idea to have class base as an abstract
base class

struct base
{
  virtual ~base() = 0;

};

What this will ensure is that you will not be allowed to catch by
value using b (even if rightly speaking that is not catching m):

int main()
{
try
{
myFunc();
}
catch(base) //This would work if the class base is not ABC.
{
}
}

 
M

Michael Doubez

I am trying to solve a weird problem because of the following design
that I want to implement.

I have an 'exception' class that no other function must be able to
throw except a select group of functions. I want the compiler to
prohibit people who use my code the capability of throwing instances
of my exception class.

The solution that works 99.99% of the time it to tell it in your
comments.
However they will still be able catch objects
of the exception class. Why do I want to do this ? This brings clarity
to the working of the code - the source of all instances of my
exception class is only one - my code. I could have easily acheived
this in Java by making the constructor of my exception class package-
private ( which I acheive by not specifying any access modifier for
the constructor ) . I would then put the exception class inside the
package where my code will be making new instances of the exception
class. But C++ lacks this kind of access modifier. Now how should I
design my exception class to achieve this in C++ ?

Make the constructor private and declare one class as friend which
will be used in your code to generate your exception. Don't publicly
expose the detail of the friend class.
I resorted to making the constructor of my exception class as private
and making all the functions of my code which will be allowed to throw
instances of this exception class, as 'friend's of the class.
A representative piece of code of this design is given here:

struct m
{
private:
m ( )
{ }

friend void myFunc ( ) throw ( m ) ;

Exception specifications are useless in c++ except to specify a
nothrow guarantee.
} ;

void myFunc ( ) throw ( m )
{

}

int main ( )
{
return 0 ;

}

The problem is that the above code doesn't compile in g++ ( GNU
version of c++ compiler ). It throws the following errors:

structure.cpp:8: error: invalid use of undefined type ‘struct m’
structure.cpp:3: error: forward declaration of ‘struct m’

Even more surprising fact is that the above code won't compile even if
I change the line 'friend void myFunc ( ) throw ( m )' to :
friend void myFunc ( ) throw ( m * )
and also make the corresponding change in the function definition of
'myFunc' !

However, it does compile only when I change this line to :
friend void myFunc ( ) throw ( m * * )
Here's the complete code:

struct m
{
private:
m ( )
{ }

friend void myFunc ( ) throw ( m * * ) ;

} ;

void myFunc ( ) throw ( m * * )
{

}

int main ( )
{
return 0 ;

}

Why does g++ allow me throw a pointer to a pointer of type 'struct m'
but not a pointer of type 'struct m' ?

Because the standard specifies it §15.4/2:
"[...]A type denoted in an exception-specification shall not denote an
incomplete type. A type
denoted in an exception-specification shall not denote a pointer or
reference to an incomplete type, other than void*, const void*,
volatile void*, or const volatile void*.[...]"
 
J

James Kanze

On 5 mai, 12:05, (e-mail address removed) wrote:
The solution that works 99.99% of the time it to tell it in
your comments.

Agreed. And even then: I can't think of the slightest valid
reason for not allowing the client code to create instances, if
it wants. It can't really hurt your code in any way.

Except that that doesn't work in Java. Unless your class is
also final, anyone can access the package access elements just
by deriving from it.
Make the constructor private and declare one class as friend
which will be used in your code to generate your exception.
Don't publicly expose the detail of the friend class.
Exception specifications are useless in c++ except to specify a
nothrow guarantee.

Agreed. (Although I'm willing to admit that there might be
exceptions, I've yet to see one.) Note that any time you'd use
an exception specification in Java (i.e. anytime it actually
means anything), you're better off using a return value in
C++ -- and usually in Java as well. (Sometimes, you'll use an
exception in Java because emulating out or inout parameters is
even more invasive. This isn't a problem in C++.)
Because the standard specifies it §15.4/2:
"[...]A type denoted in an exception-specification shall not denote an
incomplete type. A type
denoted in an exception-specification shall not denote a pointer or
reference to an incomplete type, other than void*, const void*,
volatile void*, or const volatile void*.[...]"

One does sort of wonder why this restriction. It's important
that the type be complete when the function is either called or
defined (so that the compiler can set up the necessary code to
catch violations), but certainly not when the function is
declared.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top