C++能做一些类似ML大小写表达式的事情吗
Can C++ do something like an ML case expression?
所以,我在C++中遇到过几次这种事情,我真的很想写一些类似的东西
case (a,b,c,d) of
(true, true, _, _ ) => expr
| (false, true, _, false) => expr
| ...
但在C++中,我总是会得到这样的结果:
bool c11 = color1.count(e.first)>0;
bool c21 = color2.count(e.first)>0;
bool c12 = color1.count(e.second)>0;
bool c22 = color2.count(e.second)>0;
// no vertex in this edge is colored
// requeue
if( !(c11||c21||c12||c22) )
{
edges.push(e);
}
// endpoints already same color
// failure condition
else if( (c11&&c12)||(c21&&c22) )
{
results.push_back("NOT BICOLORABLE.");
return true;
}
// nothing to do: nodes are already
// colored and different from one another
else if( (c11&&c22)||(c21&&c12) )
{
}
// first is c1, second is not set
else if( c11 && !(c12||c22) )
{
color2.insert( e.second );
}
// first is c2, second is not set
else if( c21 && !(c12||c22) )
{
color1.insert( e.second );
}
// first is not set, second is c1
else if( !(c11||c21) && c12 )
{
color2.insert( e.first );
}
// first is not set, second is c2
else if( !(c11||c21) && c22 )
{
color1.insert( e.first );
}
else
{
std::cout << "Something went wrong.n";
}
我想知道是否有任何方法可以清理所有的if和else,因为它似乎特别容易出错。如果能够像SML那样在case表达式(或C++中的语句)不详尽时让编译器抱怨,那就更好了。我意识到这个问题有点含糊。总之,如何简洁地表示C++中具有任意数量变量的穷举真值表?提前谢谢。
我喜欢Alan的解决方案,但我不同意他的结论,即它太复杂了。如果您可以访问C++11,它几乎为您提供了所需的所有工具。您只需要编写一个类和两个函数:
namespace always {
struct always_eq_t {
};
template <class lhs_t>
bool operator==(lhs_t const&, always_eq_t)
{
return true;
}
template <class rhs_t>
bool operator==(always_eq_t, rhs_t const&)
{
return true;
}
} // always
然后,您可以用与ML:相对类似的方式编写函数
#include <tuple>
#include <iostream>
void f(bool a, bool b, bool c, bool d)
{
always::always_eq_t _;
auto abcd = std::make_tuple(a, b, c, d);
if (abcd == std::make_tuple(true, true, _, _)) {
std::cout << "true, true, _, _n";
} else if (abcd == std::make_tuple(false, true, _, false)) {
std::cout << "false, true, _, falsen";
} else {
std::cout << "elsen";
}
}
int
main()
{
f(true, true, true, true);
f(false, true, true, false);
return 0;
}
在C++中,你经常想考虑是否有一种合理的类型可以帮助我更容易地编写代码?此外,我认为如果您有ML背景,您将从研究C++模板中受益匪浅。它们对于在C++中应用函数式编程风格非常有帮助。
C++传统上是面向个人的,无论语法如何,都无法执行类似以下操作。
if ([a,b,c,d] == [true,true,false, false]) {}
新C++标准有一些东西可以让你内联地定义常量数组,因此可以定义一个类,将数组作为构造函数并支持这种比较。类似的东西
auto x = multi_val({a,b,c,d});
if (x == multi_val({true, true, false, false}))
{ ... }
else if (x == multi_val(etc.))
但现在要进行像_这样的部分匹配,这是不直接支持的,你必须让你的类变得更加复杂才能篡改它,比如使用一个可能的模板类型并进行
multi_val(true, true, maybe<bool>(), maybe<bool>)
这进入了相当令人兴奋的C++领域,绝对不是我对如此初级的东西会做的。
对于C++11,假设您只想匹配固定数量的布尔值,并且可以在没有_模式匹配的情况下生存,那么[1](扩展到您需要的变量数量)。
我仍在研究一种替代解决方案,该解决方案使用模板来匹配任意类型,使用lambda或函数来匹配表达式。
-编辑-
正如承诺的那样,[2]任意类型的模式匹配,包括未指定的值。
注意几个注意事项:
- 这段代码只适用于4个变量(实际上是我第一次尝试使用模板元编程)。使用可变模板可以大大改善这一点
- 它起作用,但不是很整洁或组织良好。更多的是在引入生产代码之前需要清理的概念验证
- 我对比赛功能不满意。我希望使用初始值设定项列表来传递要计算的表达式,并在第一次匹配时停止(在当前实现中,将执行每个匹配条件)——然而,我无法快速想到如何通过单个初始值设定器列表传递不同类型的表达式匹配对象
我想不出任何一种方法来验证真值表是详尽的。
干杯,
-缺口
[1]
constexpr int match(bool v, int c)
{
return v ? (1 << c) : 0;
}
constexpr int match(bool a, bool b)
{
return match(a, 0) | match(b, 1);
}
int main()
{
int a = true;
int b = false;
switch(match(a, b))
{
case match(false, false):
break;
case match(false, true):
break;
case match(true, false):
break;
case match(true, true):
break;
}
}
[2]
template<typename V1, typename V2, typename V3, typename V4>
class pattern_match_t
{
private:
V1 value_0;
V2 value_1;
V3 value_2;
V4 value_3;
public:
typedef std::function<void(V1, V2, V3, V4)> expr_fn;
template <typename C1, typename C2, typename C3, typename C4>
pattern_match_t<V1, V2, V3, V4>& match(C1 a, C2 b, C3 c, C4 d, expr_fn fn)
{
if(value_0 == a && value_1 == b && value_2 == c && value_3 == d)
fn(value_0, value_1, value_2, value_3);
return *this;
}
pattern_match_t(V1 a, V2 b, V3 c, V4 d)
: value_0(a), value_1(b), value_2(c), value_3(d)
{
}
};
template<typename T>
class unspecified
{};
template<typename T>
constexpr bool operator==(unspecified<T>, const T&)
{
return true;
}
template<typename T>
constexpr bool operator==(const T&, unspecified<T>)
{
return true;
}
template<typename V1, typename V2, typename V3, typename V4>
pattern_match_t<V1, V2, V3, V4> pattern_match(V1 a, V2 b, V3 c, V4 d)
{
return pattern_match_t<V1, V2, V3, V4>(a, b, c, d);
}
int main()
{
bool test_a = true;
std::string test_b = "some value";
bool test_c = false;
bool test_d = true;
pattern_match(test_a, test_b, test_c, test_d)
.match(true, unspecified<std::string>(), false, true, [](bool, std::string, bool, bool)
{
return;
})
.match(true, "some value", false, true, [](bool, std::string, bool, bool)
{
return;
});
}
- (C++)分析树以计算返回错误值的简单算术表达式
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 提升精神:解析布尔表达式并简化为规范范式
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 使用正则表达式regex_search在字符串中查找字符串
- 如何确认我的constexpr表达式实际上已经在编译时执行
- 概念中的cv限定符需要表达式参数列表
- 为什么constexpr的性能比正常表达式差
- 对于结构,表达式必须是可修改的ivalue
- 当一个值是非常量但用常量表达式初始化时使用constexpr
- 将fold表达式与std::一起用于两个元组
- 断言中的Fold表达式在某些计算机上编译,但在其他计算机上不编译
- 标记 '","' 之前的预期主表达式
- 如何提升Perl正则表达式匹配,支持区分大小写/不区分大小写
- 有条件地忽略 c++11 正则表达式中的大小写
- 开关大小写错误 |的值在常量表达式中不可用
- C++能做一些类似ML大小写表达式的事情吗
- 如何在 C/C++ 中访问大小写表达式中的事例值
- 大小写表达式不是常量
- c++切换正则表达式大小写