gpt4 book ai didi

c++ - Google 测试 (gtest) `EXPECT_TRUE()` 宏不会使用 `std::is_same<>` 模板作为输入进行编译

转载 作者:行者123 更新时间:2023-12-02 16:22:02 30 4
gpt4 key购买 nike

在使用 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 .

我正在做的事情有什么问题,我怎样才能编译它?

引用资料:

  1. std::is_same<T, U>::value and std::is_same_v<T, U>
  2. GoogleTest (gtest) documentation

最佳答案

总结

这不起作用,因为处理宏的 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 being some_template<a, and the second being b>(). Since EXPECT_TRUE only accepts a single argument, this fails to compile.

In this particular case I'd recommend using static_assert instead of EXPECT_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++ 模板 <> 范围运算符),我就可以进行一些谷歌搜索并找到以下答案来查看也是:

  1. Too many arguments provided to function-like macro invocation
  2. Getting too many arguments provided to function-like macro invocation compile error while defining lambda inside assert (assert.h) in Xcode [c++]

关键字:注意模板参数输入的宏; C/C++ 宏预处理器的逗号参数定界符,宏参数周围的宏需要 C++ 额外括号

关于c++ - Google 测试 (gtest) `EXPECT_TRUE()` 宏不会使用 `std::is_same<>` 模板作为输入进行编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65300353/

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