为什么以下打印出来"A boolean!"?我意识到有一些奇怪的转换,因为如果我显式构造一个std :: string,我会得到正确的行为.但是为什么visitor::operator()(bool)在下面的情况下选择重载决策呢?
#include <boost/variant.hpp>
#include <string>
typedef boost::variant<bool, std::string> type;
struct visitor : public boost::static_visitor<> {
void operator()(bool b) const {
std::cout << "A boolean!" << std::endl;
}
void operator()(const std::string& str) const {
std::cout << "A string!" << std::endl;
}
};
int main(int argc, char* argv[]) {
type t = "I am a string";
t.apply_visitor(visitor());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我正在运行Visual Studio 2012(CTP或者没有给出相同的结果)
我正在寻找一个像foldlWithKey一样的函数,但是封装在monad中.
我希望它有类型
Monad m => (a -> k -> b -> m a) -> a -> Map k b -> m a
Run Code Online (Sandbox Code Playgroud)
但是Hoogle没有给我任何类型的东西.
为什么以下声明无效?
template<template<typename> typename T>
struct S {};
Run Code Online (Sandbox Code Playgroud)
我认为这是有效的,因为以下是有效的:
template<template<typename> class T>
struct S {};
Run Code Online (Sandbox Code Playgroud)
我可以从[gram.temp]中的标准中读到它似乎是有效的,但是gcc给了我以下输出:
prog.cpp:4:38: error: expected 'class' before 'T'
template<template<typename> typename T>
^
Run Code Online (Sandbox Code Playgroud) 我有一个带有相当多构造函数的数据类型,所有这些都很简单,Haskell可以自动派生Ord实例.如:
data Foo a
= A a
| B Int
deriving (Eq, Ord)
Run Code Online (Sandbox Code Playgroud)
现在我想添加第三个构造函数,如下所示:
data Foo a
= A a
| B Int
| C a (a -> Bool)
Run Code Online (Sandbox Code Playgroud)
但现在哈斯克尔不能手动导出Eq和Ord上Foo我.现在,碰巧我有一些关于如何构造两个值的特定领域知识C:
instance Eq a => Eq (Foo a) where
-- Boilerplate I don't want to write
A x == A y = x == y
B x == B y = x == y
-- This is the case I really care about …Run Code Online (Sandbox Code Playgroud) 我正在尝试转换以下语法生成
callExpr:
primaryExpr
| callExpr primaryExpr
Run Code Online (Sandbox Code Playgroud)
到Haskell中的Parsec表达式.
显然问题是它是左递归的,所以我试图解析它的递归 - 上升风格.我试图实现的伪代码是:
e = primaryExp
while(true) {
e2 = primaryExp
if(e2 failed) break;
e = CallExpr(e, e2)
}
Run Code Online (Sandbox Code Playgroud)
我试图将其翻译成Haskell是:
callExpr :: IParser Expr
callExpr = do
e <- primaryExpr
return $ callExpr' e
where
callExpr' e = do
e2m <- optionMaybe primaryExpr
e' <- maybe e (\e2 -> callExpr' (CallExpr e e2)) e2m
return e'
Run Code Online (Sandbox Code Playgroud)
其中primaryExprtype IParser Expr
和IParser定义为
type IParser a = ParsecT String () (State SourcePos) a
Run Code Online (Sandbox Code Playgroud)
但是这会给我以下类型错误: …
parsing haskell functional-programming parsec left-recursion
我会希望下面的代码进行编译和修改的值v1是{7, 9, 11, 13, 15}在基于范围循环后。
#include <boost/range/combine.hpp>
#include <vector>
int main()
{
std::vector<int> v1{1, 2, 3, 4, 5};
std::vector<int> v2{6, 7, 8, 9, 10};
for(auto&& [a, b] : boost::combine(v1, v2)) {
a += b;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是出现以下编译错误(带有g++ -std=c++17):
error: invalid operands to binary expression ('int' and 'boost::tuples::cons<int &, boost::tuples::cons<int &, boost::tuples::null_type> >::tail_type' (aka
'boost::tuples::cons<int &, boost::tuples::null_type>'))
a += b;
~ ^ ~
1 error generated.
Run Code Online (Sandbox Code Playgroud)
我该如何实现?