【问题标题】:Why is this "call" to assert seeing two arguments instead of just the one?为什么这个“调用”断言看到两个参数而不仅仅是一个?
【发布时间】:2016-03-10 12:01:46
【问题描述】:

给定this test program

#include <cassert>
#include <string>
#include <type_traits>

const std::string& const_string = "bla";
std::string const & string_const = "blabla";

static_assert(std::is_same<decltype(const_string), decltype(string_const)>::value, "Uhoh");

int main()
{
    assert(std::is_same<decltype(const_string), decltype(string_const)>::value);
}

使用 C 的断言断言两种类型在编译时和运行时相同。所有的 Clang、MSVC2015 和 GCC 都报告相同的错误,所以我很确定 it's me

main.cpp:13:49: error: too many arguments provided to function-like macro invocation
    assert(std::is_same<decltype(const_string), decltype(string_const)>::value);
                                                ^
/usr/include/assert.h:91:10: note: macro 'assert' defined here
# define assert(expr)                                                   \
         ^

我只是在assert 中没有看到两个参数。更重要的是,static_assert 工作得很好......那么这里发生了什么?

【问题讨论】:

  • 我相信预处理器会看到两个“比较”; std::is_same&lt;decltype(const_string)decltype(string_const)&gt;::value。预处理器无法判断它们不是。
  • static_assert 是关键字,assert 是预处理器宏。预处理器宏以非智能方式将参数拆分为,。额外的括号可以防止这种情况发生。

标签: c++ assert static-assert


【解决方案1】:

C 预处理器不识别 C++ 模板语法,因此模板括号 &lt;&gt; 不被预处理器视为分组标记,它们被视为简单字符。

这意味着预处理器会将模板参数之间的逗号视为宏参数分隔符,如下所示:

assert(
    std::is_same<decltype(const_string),
    decltype(string_const)>::value);

要强制预处理器将您的表达式视为单个语句,只需将您的 assert 参数包装在一组额外的括号中:

assert((std::is_same<decltype(const_string), decltype(string_const)>::value));

static_assert 没有这个限制,因为它是一个C++ keyword,而不是像assert() 这样的预处理器宏。这意味着它完全支持 C++ 语法并正确查看模板参数。

【讨论】:

    【解决方案2】:

    这是因为 &lt;&gt; 令牌。他们搞砸了预处理器。请记住,assert 是一个宏,而不是一个函数。

    执行此操作(添加一组额外的括号):

    assert((std::is_same<decltype(const_string), decltype(string_const)>::value));
           ^                                                                   ^
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 2018-11-04
      相关资源
      最近更新 更多