该类型和功能是否已有名称? [英] Is there an existing name for this type and function?

查看:97
本文介绍了该类型和功能是否已有名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

计算机科学中有两个棘手的问题:缓存失效,命名和一次出错.

There are 2 hard problems in computer science: cache invalidation, naming things and off-by-one errors.

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

This is about the 2nd problem: naming things.

我正在寻找这种技术或类型是否已经在其他地方使用过并且有名称. dichotomy是一个好的名称,但是bools_at_compile_time是一个可怕的名称.

I'm looking if this technique or type has been used somewhere else already and has a name. dichotomy is an ok name, but bools_at_compile_time is a horrible one.

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)... );
}

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

dichotomy_t is a variant between true and false. Its runtime representation is 0 or 1.

这让您可以做的是:

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
}

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

Is there a name for dichotomy_t or the more general bools_at_compile_time technique? I'm looking for a name that is well known in any community (even a non-C++ one), even a verb that describes "taking a runtime value and creating a switch and a set of compile time value in generated code to pick between" better than a sentence.

在线示例

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

A good answer would include the name, citations/quotes describing what that name means, examples of that named thing in use in the other context, and evidence that this name is equivalent to or inclusive of the above type/value and function.

(这可能有助于找到一个名称,将其通用化为enum而不是bool,后者具有固定数量的已知状态,以及将运行时值转换为每个case子句中的编译时常量.)

(It may help to find a name the generalization of this would be an enum instead of a bool, which has a fixed number of known states, and a switch/case map that converts the runtime value into a compile-time constant in each case clause.)

推荐答案

我不知道该模式的任何现有名称,但是如果您对STL的命名方式有所了解,则可以使用足够接近的名称使您的代码清晰明了.

I do not know of any existing names for this pattern, but if you take a good look at how the STL is naming things, you can use name close enough to make your code explicit.

我也喜欢@ Jarod42的dispatcher_t想法,我认为它比dichotomy_tn_chotomy_t更通用.

I also liked the dispatcher_t idea from @Jarod42 , I think it is more generic than dichotomy_t or n_chotomy_t.

dichotomy()可以称为make_variant(b).因为它将返回参数中给定的布尔值的std::variant值.就像std::make_tuple通过多个参数创建一个元组.

dichotomy() could be called make_variant(b). Since it will return the std::variant value of a boolean given in argument. Much like std::make_tuple makes a tuple from multiple arguments.

我建议将bools_at_compile_time替换为static_eval.就像static_assert在编译时进行断言一样.

I would suggest to replace bools_at_compile_time by static_eval. Much like static_assert makes an assertion at compile time.

如果eval对于您的用例而言不是正确的形容词,则可以轻松地对其进行调整.

Not that if eval is not the correct adjective for your use case you can easily adapt it 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";
}

这篇关于该类型和功能是否已有名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆