【发布时间】:2017-03-16 00:23:07
【问题描述】:
我使用的是过时的 Visual Studio 2008(让我省去“你的问题”的麻烦。)这似乎是 Visual Studio 的问题:http://rextester.com/XKFR77690 这似乎是assert 宏的问题:http://ideone.com/bhxMi0
鉴于这些结构:
struct base { virtual ~base() {} };
template <typename T>
struct Foo : base { T foo; };
我可以这样做:
base* test = new Foo<pair<int, int>>;
if(dynamic_cast<Foo<pair<int, int>>*>(test) != NULL) cout << "hello world\n";
但是当我在assert:assert(dynamic_cast<Foo<pair<int, int>>*>(test) != NULL) 中使用与if-statement 中完全相同的代码时,我得到一个错误:
警告C4002:宏
assert的实际参数过多
错误 C2143:语法错误:在 ')' 之前缺少 ','
顺便说一句,我可以通过使用 C 风格的演员来解决这个问题:assert((Foo<pair<int, int>>*)(test) != NULL) 但我认为 C 风格的演员会做 static_cast 而不是 dynamic_cast,这是我不想要的。
【问题讨论】:
标签: c++ visual-studio gcc assert dynamic-cast