Software Archive
Read-only legacy content
17061 Discussions

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

emke88
Beginner
718 Views

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 "$".

[cpp]
#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 { public: Foo() {} private: Value m_val; }; void Switch(int i) { pointerToAny temp; try { switch(i) { case 1: temp.reset(new boost::any(Foo())); break; case 2: throw std::runtime_error("Case 2 threw"); break; } } catch(...) { } } int main() { Switch(2); return 0; }[/cpp]

0 Kudos
6 Replies
Quoc-An_L_Intel
Moderator
718 Views

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

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

0 Kudos
Quoc-An_L_Intel
Moderator
718 Views
BTW, I got the error with boost version Boost_1_42_0.
0 Kudos
emke88
Beginner
718 Views

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;

with:

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.

0 Kudos
Quoc-An_L_Intel
Moderator
718 Views

>>> 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.

0 Kudos
Quoc-An_L_Intel
Moderator
718 Views

#include &ltstdio.h>

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 []).

0 Kudos
Quoc-An_L_Intel
Moderator
718 Views
This issue should be resolve in the latest 11.1 and Composer products.
0 Kudos
Reply