gpt4 book ai didi

C++ 断言 : the precedence of the expression in an assert macro

转载 作者:IT老高 更新时间:2023-10-28 22:02:48 31 4
gpt4 key购买 nike

在 C++ 中:

  assert(  std::is_same<int , int>::value  ); // does not compile

assert( (std::is_same<int , int>::value) ); // compiles

谁能解释一下原因?

最佳答案

assert是一个预处理器宏。预处理器宏是愚蠢的;他们不懂模板。预处理器在括号内看到 10 个标记:

assert( std :: is_same < int , int > :: value );

它以逗号分隔。它不知道这是错误的分割位置,因为它不明白 std::is_same<intint>::value不是有效的 C++ 表达式。

预处理器 足够聪明,不会在多个参数之间分解内部括号对的内容。这就是为什么添加额外的括号可以解决问题。

关于C++ 断言 : the precedence of the expression in an assert macro,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24793706/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com