- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在使用 clang 编译器的 C++17 中,无论我是否这样做,我都会得到相同的构建错误:
EXPECT_TRUE(std::is_same_v<decltype(var1), decltype(var2)>);
或者这个:
EXPECT_TRUE(typename std::is_same_v<decltype(var1), decltype(var2)>);,
或者这个:
EXPECT_TRUE(typename std::is_same_v<typename decltype(var1), typename decltype(var2)>);
构建命令:
bazel test //my_target_dir:my_target
构建错误:
error: too many arguments provided to function-like macro invocation
decltype(var2)>);
^
gtest/gtest.h:1980:9: note: macro 'EXPECT_TRUE' defined here
#define EXPECT_TRUE(condition) \
^
myfile.cpp:125:5: error: use of undeclared identifier 'EXPECT_TRUE'
EXPECT_TRUE(std::is_same_v<
^
请注意 EXPECT_TRUE()
的 Googletest 定义在这里:https://github.com/google/googletest/blob/master/googletest/include/gtest/gtest.h#L1980 .
我正在做的事情有什么问题,我怎样才能编译它?
最佳答案
这不起作用,因为处理宏的 C++ 预处理器是在模板存在之前编写的,并且将逗号视为分隔宏的两个单独参数。它认为我已经调用了 EXPECT_TRUE()
宏 anything<foo
作为第一个参数,bar>
作为第二个参数:
// This does NOT work, because the preprocessor sees this 1 template
// argument to the macro as two separate arguments separated by the
// comma
EXPECT_TRUE(anything<foo, bar>);
这些选项确实有效:
// Option 1: move the template outside of the macro call
bool isSameType = std::is_same_v<decltype(var1), decltype(var2)>;
EXPECT_TRUE(isSameType);
// Option 2: for this particular case I can instead use the
// `static_assert()` function in place of the `EXPECT_TRUE()` macro
static_assert(std::is_same_v<decltype(var1), decltype(var2)>);
// Option 3: use double parenthesis to force the macro to treat
// the parameter containing comma-separated template parameters
// as a **single argument** to the macro:
EXPECT_TRUE((std::is_same_v<decltype(var1), decltype(var2)>));
在与一些 friend 聊天后,其中一位 Drew Gross , 解释如下:
Due to the C++ preprocessor model, commas within a template instantiation are interpreted as separating arguments of the macro, not arguments of the template. This is one of the many reasons that using macros is heavily discouraged in modern C++. So when you write:
SOME_MACRO(some_template<a, b>());
it's interpreted as passing 2 arguments to
SOME_MACRO
, the first beingsome_template<a
, and the second beingb>()
. SinceEXPECT_TRUE
only accepts a single argument, this fails to compile.In this particular case I'd recommend using
static_assert
instead ofEXPECT_TRUE
. If the thing you were testing wasn't a compile time constant, you would have to assign to a local variable first, e.g.bool localVar = some_template<a, b>();
EXPECT_TRUE(localVar);
他是对的。由于 C 和 C++ 宏预处理器是在 C++ 存在之前编写的,它不识别 C++ <
和 >
模板范围符号(它认为它们分别只是“小于”和“大于”符号),所以在这个语句( EXPECT_TRUE(std::is_same_v<decltype(var1), decltype(var2)>);
)中,它看到逗号并解析 std::is_same_v<decltype(var1)
作为 gtest EXPECT_TRUE()
的第一个参数宏,和 decltype(var2)>
作为宏的第二个参数。
因此,这里是解决方案:
bool isSameType = std::is_same_v<decltype(var1), decltype(var2)>;
EXPECT_TRUE(isSameType);
然而,正如德鲁所说,更好的解决方案是只使用 static_assert()
在这种情况下而不是 gtest 的 EXPECT_TRUE()
,因为这个测试可以在编译时而不是运行时完成:
(更好的解决方案):
static_assert(std::is_same_v<decltype(var1), decltype(var2)>);
注意:static_assert()
不需要消息在 C++17 中。 See here.
我做了一些额外的研究和实验,还发现额外的括号也能解决这个问题。使用额外的括号会强制预处理器将整个输入参数识别为宏的第一个参数,因为预处理器尊重括号但不尊重模板<>
符号,因为它不支持模板。
因此,这也适用:
EXPECT_TRUE((std::is_same_v<decltype(var1), decltype(var2)>));
如有疑问,请将其括起来。如果确实需要,请加上括号。 :)
所以,现在我们有 3 个可行的解决方案来解决这个问题。我可能会选择 static_assert()
选项作为我的主要解决方案,如果我需要在运行时测试一些模板输入,则上面的额外括号选项作为我的解决方案。
一旦我知道问题的本质是什么(宏预处理器看到逗号但不识别 C++ 模板 <
和 >
范围运算符),我就可以进行一些谷歌搜索并找到以下答案来查看也是:
关键字:注意模板参数输入的宏; C/C++ 宏预处理器的逗号参数定界符,宏参数周围的宏需要 C++ 额外括号
关于c++ - Google 测试 (gtest) `EXPECT_TRUE()` 宏不会使用 `std::is_same<>` 模板作为输入进行编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65300353/
如何在 Excel 中编写可以在我将打开的任何 Excel 文档上工作(使用快捷方式运行)的宏? 这可能吗? 最佳答案 您需要将宏添加到 Personal.xlsb 以使它们可用于所有 excel 文
我正在研究 problem #74在4clojure.com,我的解决方案如下: (defn FPS [s] (->> (map read-string (re-seq #"[0-9]+"
我还没有完全理解Clojure 箭头宏thread-first -> 和thread-last 宏->> 之间的区别。在阅读 https://clojure.org/guides/threading_
我想将一些调试输出语句插入到大型 C 代码库中。这些调试输出语句将由编译器选项开关控制。 调试输出语句如下所示: #ifdef DEBUG_FLAG Print(someSymbol) #endif
我正在通过宏将代码注入(inject)到 C++ 类中。有没有办法根据访问修饰符的上下文来做到这一点?有点像 #if (we_are_in_public_context) INJECT_PUBLIC_
这应该与 memoize 类似,但有很大不同。虽然 memoize 应该与纯函数一起使用,但它通常对加速 IO 相关函数很有用。 我正在寻找的函数/宏应该表现得像高阶函数。它产生的功能应该: 第一次调
对于下面的代码: let services: [MyServices] = [ MyService(), #if DEBUG DebugService(), #endi
假设我有以下文本文件 name: John Doe description: My name is John Doe and I'm really good at vim! name: John Do
在创建 Excel 宏方面需要帮助。我有一个 Excel 工作表。Excel 工作表不一致。我打算使它统一和结构化。 例如。 A B C
我正在 excel 中设置一个宏,以便在更新单元格时自动发送电子邮件。是否可以在电子邮件正文中包含单元格的内容?例如,如果单元格 G7 已更新,请在电子邮件中包含单元格 B7 的内容?单元格行将是相同
我创建了一个简单的 Excel 工作表。 这是我的宏代码: Sub MyMacro() Sheets("Sheet1").Select A$ = Cells(1, 1) Msg
在 Excel 的 VB 宏中,如何删除所有出现的以某个字符串开头的单词? 例如: 字符串内容为:xxxx $AUD543.43 yyyy 我想搜索以 $AUD 开头的字符串中的任何内容并删除下一个空
我是 Excel 宏的新手.. 谁能告诉我这个宏是做什么的? Sub People_Add_Document() prow = ActiveCell.row num = Cells(p
我对 Excel 中的 VBA 和宏非常陌生。我有一个非常大的 Excel 电子表格,其中 A 列保存日期。我正在尝试删除值小于某个日期的行,这就是我到现在为止的想法。 Sub DELETEDATE(
我在 Excel 2003 中有一个 VBA 对象,当通过流数据获得某些值时,它会触发三个简单的宏。它运行良好。我想打开一个重复的工作表,但具有不同的流数据,并在各自的工作表上触发宏。它现在可以使用,
下面的宏有什么问题?我只想评估一个选项卡中的一个单元格是否大于另一个选项卡中的另一个单元格。然后消息框: Sub Comhouse() If Worksheets("(2.2) TRA works
需要一个简单的 excel 宏的帮助。我在第 1 列 X1 到 X20 中有数据。我想自动将此信息粘贴到 A 列,然后当我更新 X 列中的数字时,我想将此信息粘贴到 B 列,然后再粘贴到 C 列...
我找到了以下代码,效果很好;但是,我必须手动更改月份,以便它转到第二个工作簿的右侧工作表。由于工作表以月为单位,我怎样才能使其自动更改为当月? Sub AlarmSheet() Dim wkb As
很难说出这里问的是什么。这个问题是模棱两可的、模糊的、不完整的、过于宽泛的或修辞的,无法以目前的形式得到合理的回答。如需帮助澄清这个问题以便重新打开它,visit the help center .
我的公司只使用 MS Office 2003 产品,所以我必须坚持下去。由于我的工作性质,我需要使用很多“复制和粘贴”功能。源数据主要来自网站,我将数据粘贴到 Excel 中的单元格中。问题是剪贴板保
我是一名优秀的程序员,十分优秀!