Maybe在我的工作项目中,我反复发现自己需要Haskell风格(尤其是Maybe chaining).例如,来自客户的提款请求,我们获得客户ID ...在缓存中查找客户...如果找到客户...查找她的储蓄账户...如果有账户...退出...在任何指向此链中,如果存在查找失败,则不执行任何操作并返回失败.
我的链很大......有时只要6 ...所以这里是我Haskell.Data.Maybe在C++ 0x中的滑动...(注意......如果我停止使用可变参数模板,这应该在C++中工作).我已经开始为自由函数链接一个参数或成员函数没有参数,我对接口感到满意.但是,对于采用多个参数的函数...我必须编写一个lambda函数来模拟部分应用程序.有没有办法避免它?见最后一行main().即使它是未注释的,它也不会编译,而是用于const/non-const混合.但问题仍然存在.
对于大量的代码感到抱歉...我希望这不会让那些可能对此感兴趣的人感到震惊......
#include <iostream>
#include <map>
#include <deque>
#include <algorithm>
#include <type_traits>
typedef long long int int64;
namespace monad { namespace maybe {
struct Nothing {};
template < typename T >
struct Maybe {
template < typename U, typename Enable = void >
struct ValueType {
typedef U * const type;
};
template < typename U >
struct ValueType < U, typename std::enable_if < std::is_reference < U >::value >::type > …Run Code Online (Sandbox Code Playgroud) 我实际上试图看看我是否可以获得一个最小的库,它支持我在boost :: fusion中使用的极少数操作.
这是我到目前为止所拥有的......
template < typename... Types >
struct typelist
{
};
template < template < typename... > class F, typename... Args >
struct apply
{
typedef typename F < Args... >::type type;
};
template < typename, template < typename... > class >
struct foreach;
template < typename... Types, template < typename Arg > class F >
struct foreach < typelist < Types... >, F >
{
typedef typelist < typename apply < F, Types >::type... > type;
}; …Run Code Online (Sandbox Code Playgroud) 我正在使用std::transforman std::back_inserter来追加元素std::deque.现在转换可能会失败,并且boost::optional在某些情况下将返回无效对象(比如未初始化或空指针).我想过滤掉附加的无效对象.
我想过使用boost::filter_iterator,但不知道如何呈现end()过滤范围的参数.
文档boost::filter_iterator建议输出过滤是可能的.如果我只是专注operator ==于std::back_insert_iterator在这种情况下总是返回false?
除此之外,如果我想追加初始化boost::optional或指针的值,我可以链接boost::filter_iterator和boost::indirect_iterator?
我试图避免推出我自己的transform_valid带有可选extractor功能的函数.
甚至可以filter_iterator用作输出迭代器吗?
我有习惯(?!?!?)将所有内容作为"const"值返回.像这样...
struct s;
s const make_s();
s const &s0 = make_s();
s const s1 = make_s();
Run Code Online (Sandbox Code Playgroud)
使用移动操作和r值引用以及以下功能......
void take_s(s &&s0);
void take_s(s const &&s0); // Doesn't make sense
Run Code Online (Sandbox Code Playgroud)
我不能再写了......
take_s(make_s());
Run Code Online (Sandbox Code Playgroud)
我开始使用返回const值的约定的主要原因是为了防止有人编写这样的代码......
make_s().mutating_member_function();
Run Code Online (Sandbox Code Playgroud)
用例如下......
struct c_str_proxy {
std::string m_s;
c_str_proxy(std::string &&s) : m_s(std::move(s)) {
}
};
c_str_proxy c_str(std::string &&s) {
return c_str_proxy(s);
}
char const * const c_str(std::string const &s) {
return s.c_str();
}
std::vector < std::string > const &v = make_v();
std::puts(c_str(boost::join(v, ", ")));
std::string const my_join(std::vector < std::string > …Run Code Online (Sandbox Code Playgroud) 我正在尝试Fusion并发现了一些非常奇怪的东西......这是代码......我用// ############来突出显示有问题的代码.在这里麻烦######
#include <tr1/cstdint>
#include <tr1/functional>
#include <string>
#include <iostream>
// #define FUSION_MAX_VECTOR_SIZE 64
#define BOOST_MPL_LIMIT_STRING_SIZE 128
#include <boost/type_traits.hpp>
#include <boost/mpl/string.hpp>
#include <boost/fusion/algorithm.hpp>
#include <boost/fusion/tuple.hpp>
#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/container/generation.hpp>
#include <boost/fusion/container/generation/vector_tie.hpp>
typedef std::tr1::int32_t int32;
typedef std::tr1::int64_t int64;
template < class type_const_ref >
struct remove_const_reference
{
typedef typename boost::remove_reference < type_const_ref >::type type_const;
typedef typename boost::remove_const < type_const >::type type;
};
template < class T >
class MetaClass;
namespace fusion = boost::fusion;
template < class T >
struct ConstRefFieldMap
{
typedef …Run Code Online (Sandbox Code Playgroud)