加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 为什么这个“调用”断言看到两个参数而不仅仅是一个?

发布时间:2020-12-16 03:35:17 所属栏目:百科 来源:网络整理
导读:鉴于 this test program: #include cassert#include string#include type_traitsconst std::string const_string = "bla";std::string const string_const = "blabla";static_assert(std::is_samedecltype(const_string),decltype(string_const)::value,"Uh
鉴于 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)                                                   
         ^

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

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读