- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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]
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
>>> 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
#include <stdio.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 []).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page