小编rig*_*old的帖子

试图用Boost-Spirit解析SQL语句

我是boost :: spirit的新手.我编写了程序来解析一个SQL语句,比如"select*from table where conditions".它编译失败.报告了大量模板错误.那么,有人会帮助我吗?

#include <iostream>
#include <string>
#include <boost/spirit/include/qi.hpp>

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;

struct db_select {
    void exec() {}

    std::string filed;
    std::string table;
    std::string condition;
};

std::ostream& operator<<(std::ostream& os, const db_select& se) {
    return os << "filed: " << se.filed << " table: " << se.table << " condition: " << se.condition;
}

template <class Iterator>
struct selecter : qi::grammar<Iterator, db_select (), ascii::space_type> {
    selecter() : selecter::base_type(se) {
            se %= "select" >> +qi::char_ …
Run Code Online (Sandbox Code Playgroud)

c++ boost-spirit

2
推荐指数
1
解决办法
925
查看次数

实例声明Haskell

我的代码是这样的:

type Code = [Inst]

data Inst = PUSH Int
          | PUSHV Name
          | POP Name
          | DO Op
          | JUMP Label
          | JUMPZ Label
          | LABEL Label
          deriving Show

doJump :: Inst -> Code -> Code
doJump l c = case (elemIndex l c) of
                 Just n -> drop (n+1) c
                 Nothing -> c
Run Code Online (Sandbox Code Playgroud)

并且GHC给我发回了这个错误,这让我完全陷入困境......我想要做的是从特定元素发生后的点返回列表.

No instance for (Eq Inst) arising from a use of `elemIndex'
Possible fix: add an instance declaration for (Eq Inst)
In the expression: (elemIndex …
Run Code Online (Sandbox Code Playgroud)

haskell

2
推荐指数
1
解决办法
99
查看次数

记录使用无点样式定义的函数

在Clojure中创建库时,最好在每个函数中包含docstrings和其他元数据,例如:

(defn ^Boolean foo
  "Returns whether x is bar."
  {:added "1.5"}
  [x]
  (bar? x))
Run Code Online (Sandbox Code Playgroud)

有时(使用高阶函数时)最终使用def (something-that-returns-a-fn)这样的函数更容易定义函数:

(defn wrapper [f]
  "Some HOF, let's say it just prints 'WHARRGARBL' and then returns the fn."
  (println "WHARRGARBL")
  f)

(def foo
  "Prints 'WHARRGARBL' and then returns whether x is bar."
  (wrapper (fn [x] (bar? x))))
Run Code Online (Sandbox Code Playgroud)

如果我没有弄错的话,以这种方式定义函数会消除使用的优点defn- 即,文档字符串以一种很好的方式打印,而不是包括函数支持的arities,以及在函数定义中简洁地包含属性映射的能力.我是对的,还是有其他简洁的方法来记录通过HOF创建的功能?我可以这样做:

(defn foo
  "Prints 'WHARRGARBL' and then returns whether x is bar."
  {:added "1.5"}
  [x]
  ((wrapper (fn [y] (bar? y))) x))
Run Code Online (Sandbox Code Playgroud)

但这似乎有点多余而且不必要地复杂,因为我将x的函数定义为y的函数,应用于x.有没有更好的办法?

metadata docstring clojure

2
推荐指数
1
解决办法
198
查看次数

为什么不能将字符串文字传递给使用模板参数的函数?

#include<iostream>
#include<string>
using namespace std;

template<typename T>
struct Node{
    T data;
    Node* left;
    Node* right;

    Node(T x) : data(x), left(NULL), right(NULL){}
};

template<typename T>
Node<T>* new_node(T x)
{
    Node<T>* return_node = new Node<T>(x);
    return return_node;
}

int main()
{
    Node<string>* root = new_node("hi"); //error!

    string x = "hi";
    Node<string>* root2 = new_node(x); //OK
}
Run Code Online (Sandbox Code Playgroud)

为什么不能在括号内使用字符串文字?有没有简单的方法来完成相同的任务而不声明字符串然后创建节点,或者这是唯一的方法吗?

c++

2
推荐指数
1
解决办法
73
查看次数

std :: array <T,N>的大小是否保证等于T [N]的大小?

看起来这个代码工作(所以它编译得很好),我在这里问的是:它是否保证sizeof(std :: array)与sizeof(equivalent_Carray)相同?

struct MyClass {
  std::array<float, 4> arr;  
  float carr[4];

  std::array<float, 4> cfunction() {
    std::array<float, sizeof(carr) / sizeof(float)> out;
    return out;
  }

  std::array<float, 4> function() {
    // Is this guaranteed to be the same as sizeof(carr) / sizeof(float)??
    std::array<float, sizeof(arr) / sizeof(float)> out;
    std::cout << sizeof(arr);
    return out;
  }
};

int main()
{
    MyClass obj;
    obj.function();
}
Run Code Online (Sandbox Code Playgroud)

c++ arrays sizeof c++11 c++14

2
推荐指数
1
解决办法
744
查看次数

Dotty desugar多态方法如何?

据报道,Dotty将具有类型参数的类与具有类型成员的类相关联,例如:

class C[T, U] { }
// <=>
class C {
  type C$T
  type C$U
}
Run Code Online (Sandbox Code Playgroud)

Dotty desugar多态方法如下例所示?

def m[T, U](x: T, u: U): T = x
// <=>
?
Run Code Online (Sandbox Code Playgroud)

scala dotty

2
推荐指数
1
解决办法
178
查看次数

从stl列表中删除对象指针

请考虑以下代码,该代码导致访问冲突:

for(std::list<ProjectileNode*>::iterator it = m_Projectiles.begin(); it!=m_Projectiles.end(); it++)
{
    if(!(*it)->isActive()) //isActive returns a bool
    {
        m_Projectiles.remove((*it));
    }
}
Run Code Online (Sandbox Code Playgroud)

if(!(*it)->isActive()) 导致异常.

我是新手使用列表,可以使用一些帮助.指向的对象在其他地方进行管理,因此我不希望它们被销毁,只是从列表中删除,而不会导致此异常

c++ stl

1
推荐指数
2
解决办法
3340
查看次数

haskell定义新类型

这可能是一个愚蠢的问题,但在我发布之前,我花了四个小时来指出问题所在.

data Film = Film {title :: String
                ,name :: String
                ,year :: Int}
    deriving (Show)
testDatabase :: [Film]
 testDatabase = [ ("Blade Runner", "Ridley Scott",1982)]
 --(i) Add new film to the database
addFilm :: String -> String -> Int -> [Film] -> [Film]
 addFilm title director year film = film + Film title director year 

 --(ii) Give all film in the database
getFilm :: [Film]
getFilm =  testDatabase
Run Code Online (Sandbox Code Playgroud)

我想要做的是定义一个新的类型名称Film,其中包含:电影片名,电影导演和制作年份.
Testdatabase用于存储数据.
addFilm是一个向数据库添加更多电影的功能.
getfilm用于打印出电影列表.

这就是错误的样子.

coursework.hs:21:18: …
Run Code Online (Sandbox Code Playgroud)

haskell

1
推荐指数
1
解决办法
168
查看次数

定义haskell的平均值

如何在[Float]不使用递归的情况下使用类型定义均值?也给出小数点后两位的答案.我是Haskell的新手,所以任何帮助都会非常感激.即mean :: [Float] -> Float.

因为mean xs = sum xs / length xs,我得到以下内容:

 No instance for (Fractional Int)
   arising from a use of `/' at test.hs:8:10-27
 Possible fix: add an instance declaration for (Fractional Int)
 In the expression: sum xs / length xs
 In the definition of `mean': mean xs = sum xs / length xs
Run Code Online (Sandbox Code Playgroud)

haskell

1
推荐指数
1
解决办法
131
查看次数

如何在类模板之外定义类模板的构造函数模板?

考虑以下:

template<typename R>
struct call {
    template<typename F, typename... Args>
    explicit call(F&& f, Args&&... args);

    R result;
};

template<typename R, typename F, typename... Args>
call<R>::call(F&& f, Args&&... args)
: result(std::forward<F>(f)(std::forward<Args>(args)...)) { }
Run Code Online (Sandbox Code Playgroud)

clang对我大吼:

utility.tpp:40:1: error: too many template parameters in template redeclaration
template<typename R, typename F, typename... Args>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
utility.hpp:36:5: note: previous template declaration is here
    template<typename R>
    ^~~~~~~~~~~~~~~~~~~~

我完全不解.有可能吗?

c++ syntax templates

1
推荐指数
1
解决办法
384
查看次数

标签 统计

c++ ×5

haskell ×3

arrays ×1

boost-spirit ×1

c++11 ×1

c++14 ×1

clojure ×1

docstring ×1

dotty ×1

metadata ×1

scala ×1

sizeof ×1

stl ×1

syntax ×1

templates ×1