Exception catching problem when using boost::variant and boost::any

Exception catching problem when using boost::variant and boost::any

Hello there,

I've run into a problem involving exception handling when using boost::any, boost::variant and the Intel Parallel Composer C++ compiler. Below is a simple example that recreates the issue.

The problem is that even though the switch statement is wrapped in a try-catch block, if case 2 throws an exception the exception is not caught by the catch block and the program terminates.

This appears to be caused by the line "temp.reset(new boost::any(Foo()))" in case 1 and the fact that the Foo class has a member that is a boost::variant. If Foo has no such member the example below behaves as expected (i.e the exception that is thrown is caught by the catch block). The code below also behaves correctly when using MSVC.

EDIT - I've had no luck in getting the forum to accept angled brackets - they are just getting removed along with whatever they contain. I have replaced them with the symbol "$".

#include $stdexcept$
#include $memory$
#include $boost/variant.hpp$
#include $boost/any.hpp$

typedef boost::variant$ float, bool $ Value;
typedef std::tr1::shared_ptr$boost::any$ pointerToAny;

class Foo

	Value m_val;

void Switch(int i)
	pointerToAny temp;

		case 1:
			temp.reset(new boost::any(Foo()));

		case 2:
			throw std::runtime_error("Case 2 threw");

int main()
	return 0;

7 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

I get syntax error for the edited code below. I replaced the $ with angle brackets.

typedef std::tr1::shared_ptr $ boost::any $ pointerToAny;

u72319>cl -c test.cpp -EHsc

Microsoft 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86

Copyright (C) Microsoft Corporation. All rights reserved.


test.cpp(8) : error C3083: 'tr1': the symbol to the left of a '::' must be a type

test.cpp(8) : error C2039: 'shared_ptr' : is not a member of 'std'

test.cpp(8) : error C2143: syntax error : missing ';' before '<'

test.cpp(8) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

BTW, I got the error with boost version Boost_1_42_0.

Thanks for your reply.

You're getting that syntax error because the compiler you're using has no support for tr1. My original problem can still be reproduced when using a boost::shared_ptr so I suggest you replace the line:

typedef std::tr1::shared_ptr $ boost::any $ pointerToAny;


typedef boost::shared_ptr $ boost::any $ pointerToAny;

You'll also need to add an additional include directive:

#include $ boost/shared_ptr.hpp $

Hope this works for you.

>>> You're getting that syntax error because the compiler you're using has no support for tr1...

MSVS2008 compiler also fails to compile the source file using boost version1_42_0

I was able to compile without modifying the source as suggested using boost version 1_35_0, and I was able to reproduce the error.

#include &ltstdio.h&gt

Angle brackets and ampersand are escape character for html. To get them to show correctly, I had to do the following:

#include [ampersand symbol][lt]stdio.h[ampersand symbol][gt] (leave out the square brackets []).

This issue should be resolve in the latest 11.1 and Composer products.

Leave a Comment

Please sign in to add a comment. Not a member? Join today