小编rnd*_*gen的帖子

触发过程的一致性(行触发前)Postgresql

使用Postgresql.

我尝试使用TRIGGER程序对INSERT进行一些一致性检查.

问题是 ......

是否"在插入每行之前"是否可以确保每行插入"已检查"和"插入"一个接一个?我是否需要在桌面上额外锁定才能从并发插入中生存?

检查新的row1 - >插入row1 - >检查新的row2 - >插入row2

--
--

-- unexpired product name is unique.
CREATE TABLE product (
  "name"    VARCHAR(100) NOT NULL,
  "expired" BOOLEAN      NOT NULL
);

CREATE OR REPLACE FUNCTION check_consistency() RETURNS TRIGGER AS $$
  BEGIN
    IF EXISTS (SELECT * FROM product WHERE name=NEW.name AND expired='false') THEN
      RAISE EXCEPTION 'duplicated!!!';              
    END IF;
    RETURN NEW;
  END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trigger_check_consistency
BEFORE INSERT ON product
  FOR EACH ROW EXECUTE PROCEDURE check_consistency();

--
INSERT …
Run Code Online (Sandbox Code Playgroud)

postgresql triggers stored-procedures

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

模板参数类型推导在函数对象中不起作用

我有一个模板函数,带有模板化参数类型的另一个函数.

使用它时,我必须明确指定参数类型(1)否则不会编译(2).

template<typename T>
void process(const std::function<void(const T &)>& f)
{
   // ...
}

process<Foo>( [&](const Foo& arg){/*...*/} ); // (1) Ok!

// process( [&](const Foo& arg){/*...*/} ); // (2) Won't Work! 
Run Code Online (Sandbox Code Playgroud)

让(2)工作有什么诀窍吗?

c++ templates std-function type-deduction

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

在其他范围内为指针创建实例

我有两种方法来为指针创建实例.但其中一个会失败.

class A {
public:
    int num;
};

void testPointer1(A* a){
    a = new A();
    a->num = 10;
}
A* testPointer2(){
    A* a = new A();
    a->num = 10;
    return a;
}
void testPointer() {
    A* a1 = NULL;
    testPointer1(a1); // this one fails
    //cout << a1->num << endl; // segmentation fault

    A* a2 = NULL;
    a2 = testPointer2();
    cout << a2->num << endl;
}
Run Code Online (Sandbox Code Playgroud)

为什么testPointer1错了?

c++ pointers

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

通过对象创建C++奇怪的分段错误

启动一个类对象我有一个奇怪的问题.这个问题很奇怪,不易再现.但是我会尝试给出一个指示示例.我有继承类.

class BarClass {
public:
   BarClass() {
      ...
   }
   BarClass(int i, int j) {
      ...
   }
   void doSomething() { ... }
};
class FooClass : public BarClass {
public:
   FooClass() {
   }
   FooClass(int i, int j) : BarClass(i,j) {
      ...
   }
};
Run Code Online (Sandbox Code Playgroud)

有时如果我以下面的方式启动对象,我会通过初始化得到分段错误错误.

FooClass foo1;
foo1.doSomething();
FooClass foo2(10, 20);
foo2.doSomething();
Run Code Online (Sandbox Code Playgroud)

如果我使用显式指针new,那就没关系..

FooClass *foo1= new FooClass();
foo1->doSomething();
FooClass foo2(10, 20);
foo2.doSomething();
Run Code Online (Sandbox Code Playgroud)

以下代码将在第2行给出编译器错误.

FooClass foo1();
foo1.doSomething();
FooClass foo2(10, 20);
foo2.doSomething();
Run Code Online (Sandbox Code Playgroud)

我应该如何正确地启动一个对象,特别是当它有默认构造函数和带参数的对象时.

c++ constructor class object segmentation-fault

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