该类型和功能是否已有名称?

Yak*_*ont 24 c++ boolean variant c++17

在计算机科学中,有两个难题:缓存失效,命名和一个错误。

这是关于第二个问题的:命名事物。

我在寻找这种技术或类型是否已经在其他地方使用并且有名称。 dichotomy是一个不错的名字,但那bools_at_compile_time是一个可怕的名字。

using dichotomy_t = std::variant<std::false_type, std::true_type>;
// (or a struct that inherits from that, and overloads operator bool())

constexpr dichotomy_t dichotomy( bool b ) {
  if (b) return std::true_type{};
  return std::false_type{};
}
template<class F, class...Bools>
constexpr auto bools_at_compile_time( F&& f, Bools...bools ) {
  static_assert( (std::is_same<Bools, bool>{} && ...) );
  return std::visit( std::forward<F>(f), dichotomy(bools)... );
}
Run Code Online (Sandbox Code Playgroud)

dichotomy_t是true和false之间的变体。其运行时表示为01

这可以让您做的是:

auto foo( bool x, bool y ) { // <-- x and y are run-time bools here
  auto func = [&](auto x, auto y) {
    return some_template<x,y>(); // <-- x and y are compile-time bools here
  };
  return bools_at_compile_time( func, x, y ); // <-- converts runtime to compile time bools
}
Run Code Online (Sandbox Code Playgroud)

是否有名称dichotomy_t或更通用的bools_at_compile_time技术?我正在寻找一个在任何社区(甚至是非C ++社区)中都广为人知的名称,甚至是一个动词,它描述了“在生成的代码中获取运行时值并创建一个开关和一组编译时值,以在比一句话更好。

现场例子

一个好的答案应该包括名称,描述该名称含义的引文/引号,该名称在其他情况下使用的示例,以及证明该名称等同于或包含上述类型/值和功能的证据。

(这可能有助于找到一个名称,将其通用化为,enum而不是bool具有固定数量的已知状态的a和一个switch / case映射,该映射在每个case子句中将运行时值转换为编译时常量。 )

Clo*_*onk 9

我不知道该模式的任何现有名称,但是如果您很好地了解STL如何命名事物,则可以使用足够接近的名称来使代码明确。

我也很喜欢dispatcher_t@ Jarod42 的想法,我认为它比dichotomy_tor 更通用n_chotomy_t

dichotomy()可以被称为make_variant(b)。因为它将返回std::variant参数中给定的布尔值。很像std::make_tuple从多个参数组成一个元组。

我会建议更换bools_at_compile_timestatic_eval。很像static_assert在编译时声明。

并不是说,如果eval不是针对您的用例的正确形容词,您就可以轻松地对其进行调整static_*

#include <type_traits>
#include <variant>
#include <utility>

using dichotomy_t = std::variant<std::false_type, std::true_type>;
// (or a struct that inherits from that, and overloads operator bool())

constexpr dichotomy_t make_variant( bool b ) {
  if (b) return std::true_type{};
  return std::false_type{};
}
template<class F, class...Bools>
constexpr auto static_eval( F&& f, Bools...bools ) {
  static_assert( (std::is_same<Bools, bool>{} && ...) );
  return std::visit( std::forward<F>(f), make_variant(bools)... );
}

template<bool x, bool y>
auto some_template() {
    return x || y;
}

auto foo( bool x, bool y ) { // <-- x and y are run-time bools here
  auto func = [&](auto x, auto y) {
    return some_template<x,y>(); // <-- x and y are compile-time bools here
  };
  return static_eval( func, x, y ); // <-- converts runtime to compile time bools
}

#include <iostream>

int main() {
    std::cout << foo( true, true ) << "\n";
}
Run Code Online (Sandbox Code Playgroud)


Oli*_*liv 5

函数的专用版本的生成称为克隆。(请参阅过程克隆)。术语“ 克隆”用于命名优化器在恒定传播期间生成的专用功能(请参阅gcc doc)。

由生成的专用功能std::visit可以称为克隆集

将为参数值的所有组合生成此集合。该术语组合使我们假设每个参数的可能值的集合是有限的。

因此,我们可以为克隆集起一个长名称,例如,所有参数值组合的克隆集。另一个模糊但较短的选择可能是组合克隆集

正如已经指出的那样,根据参数选择正确的函数进行调用的动作可以称为dispatch

所以我建议combinatiorial_clone_set_dispatch还是dispatch_in_combinatorial_clone_set...