繁体   English   中英

在 C++ 中干净地将 void 处理为 labmda 的通用返回类型

[英]Cleanly handling void as generic return type of labmda in C++

我正在尝试像这样编写 function :

template <typename T>
void testActionBehavesIdentically(Foo& fooA, Foo& fooB, std::function<T(Foo&)> action)
  if (std::is_same<T, void>::value)
  {
    // perform action even if T is void
    action(fooA);
    action(fooB);
  }
  else
  {
    // if T is not void, test also return values
    T resultA = action(fooA);
    T resultB = action(fooB);
    CHECK_EQUAL(resultA, resultB);
  }
  // tests that the state of both foos is the same
  CHECK_EQUAL(fooA, fooB);
}

其中T有时是void 编译(在 VS2019 上)失败并error C2182: 'resultA': illegal use of type 'void' 有干净的方法吗? (希望能在大多数标准编译器中编译)? 谢谢你。

现在发生的事情是编译器仍然会编译两个if分支(因为它实际上不知道在运行时之前会调用哪个分支)。 这会导致失败,因为其中一个分支无法正确编译。 有几个修复(来自评论):

如果您的编译器支持它,这将是一种选择:

  if constexpr (std::is_same<T, void>::value) {
    // perform action even if T is void
    action(fooA);
    action(fooB);
  } else {
    ...
  }

这实际上只会根据 T 的类型编译一个分支。

如果您的编译器不支持它,这里有另一种选择:

template <typename T>
void testActionBehavesIdentically(Foo& fooA, Foo& fooB, std::function<T(Foo&)> action) {
    // check assuming T != void
}

// overload which will be called if T was void
void testActionBehavesIdentically(Foo& fooA, Foo& fooB, std::function<void(Foo&)> action) {
    // check assuming T == void
}

当 T 为 void 时,编译器将匹配重载,而不是分派到 function 的通用版本。 在这里你甚至可以说“如果 T 是一个int做一些不同的事情”,这有点整洁(但可能会变得混乱)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM