为什么 "call" 断言看到两个参数而不是一个?

Why is this "call" to assert seeing two arguments instead of just the one?

给定 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 工作得很好......那么这里发生了什么?

这是因为 <> 标记。他们搞砸了预处理器。请记住 assert 是宏,而不是函数。

这样做(添加一组额外的括号):

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

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

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

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++ 语法并正确查看模板参数。