I'm using the antiquated Visual Studio 2008 (let me save you the trouble "there's your problem".) This seems to be a problem with Visual Studio: http://rextester.com/XKFR77690 This seems to be a problem with the assert
macro: http://ideone.com/bhxMi0
Given these structs:
struct base { virtual ~base() {} };
template <typename T>
struct Foo : base { T foo; };
I can do this:
base* test = new Foo<pair<int, int>>;
if(dynamic_cast<Foo<pair<int, int>>*>(test) != NULL) cout << "hello world\n";
But when I use the exact same code as is in the if
-statement in an assert
: assert(dynamic_cast<Foo<pair<int, int>>*>(test) != NULL)
I get an error:
warning C4002: too many actual parameters for macro
assert
error C2143: syntax error : missing ',' before ')'
Incidentally I can fix this by using a C-style cast: assert((Foo<pair<int, int>>*)(test) != NULL)
But I think that the C-Style cast will do a static_cast
not a dynamic_cast
which I don't want.
Yup: macros treat top-level commas as argument separators. The simplest fix is to put parentheses around the offending code:
assert((dynamic_cast<Foo<pair<int, int>>*>(test)) != NULL)
or, if you prefer, parentheses around the entire content:
assert((dynamic_cast<Foo<pair<int, int>>*>(test) != NULL))
The reason the C-style cast in the question compiles is not that it's a C-style cast, but that it puts the template code in parentheses, so the comma is no longer at the outermost level.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With