小编Joh*_*han的帖子

为什么不调用移动构造函数?

我正在使用C++ Primer第5版进行练习,如下所示:

练习13.50:将print语句放在String类的移动操作中, 并从第13.6.1节(第534页)中的练习13.48重新运行程序,该练习使用向量来查看何时避免复制.(P.544)

String是一个练习的类,其行为类似于std::string不使用任何模板.该String.h文件中:

class String
{
public:
    //! default constructor
    String();

    //! constructor taking C-style string i.e. a char array terminated with'\0'.
    explicit String(const char * const c);

    //! copy constructor
    explicit String(const String& s);

    //! move constructor    
    String(String&& s) noexcept;

    //! operator =
    String& operator = (const String& rhs);

    //! move operator =     
    String& operator = (String&& rhs) noexcept;

    //! destructor
    ~String();

    //! members
    char* …
Run Code Online (Sandbox Code Playgroud)

c++ constructor stl move-constructor c++11

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

Kombu以非阻塞的方式

我正在寻找一种方法来使用kombu作为tornado-sockjs和Django应用服务器之间的MQ适配器.我做了类似的事情:

class BrokerClient(ConsumerMixin):
    clients = []

    def __init__(self):
        self.connection = BrokerConnection(settings.BROKER_URL)
        self.io_loop = ioloop.IOLoop.instance()
        self.queue = sockjs_queue
        self._handle_loop()

    @staticmethod
    def instance():
        if not hasattr(BrokerClient, '_instance'):
            BrokerClient._instance = BrokerClient()
        return BrokerClient._instance

    def add_client(self, client):
        self.clients.append(client)

    def remove_client(self, client):
        self.clients.remove(client)

    def _handle_loop(self):
        try:
            if self.restart_limit.can_consume(1):
                for _ in self.consume(limit=5):
                    pass
        except self.connection.connection_errors:
            print ('Connection to broker lost. '
             'Trying to re-establish the connection...')
        self.io_loop.add_timeout(datetime.timedelta(0.0001), self._handle_loop)

    def get_consumers(self, Consumer, channel):
        return [Consumer([self.queue, ], callbacks=[self.process_task])]

    def process_task(self, body, message):
        for client in self.clients:
            if …
Run Code Online (Sandbox Code Playgroud)

python tornado kombu

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

Flask-测试和 Flask-SQLAlchemy:first_or_404()

我正在尝试使用Flask-Testing Flask-SQLAlchemy模型进行测试。更准确地说,该模型使用静态方法first_or_404(),但我无法找到使我的测试工作的方法。

这是一个突出问题的独立示例:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask.ext.testing import TestCase

db = SQLAlchemy()

class ModelToTest(db.Model):
    __tablename__ = 'model_to_test'
    identifier = db.Column(db.String(80), unique=True, nullable=False, primary_key=True)

    @staticmethod
    def get_by_identifier(identifier):
        return ModelToTest.query.filter_by(identifier=identifier).first_or_404()

class Config:
    DEBUG = True
    TESTING = True
    SQLALCHEMY_DATABASE_URI = 'sqlite:///'
    SQLALCHEMY_TRACK_MODIFICATIONS = False

class TestGetByIdentifier(TestCase):

    def create_app(self):
        app = Flask('test')
        app.config.from_object(Config())
        db.init_app(app)
        return app

    def setUp(self):
        db.create_all()

    def tearDown(self):
        db.session.remove()
        db.drop_all()

    def test_get_by_identifier(self):
        self.assert404(ModelToTest.get_by_identifier('identifier'))
Run Code Online (Sandbox Code Playgroud)

我收到错误:

(my_env) PS C:\Dev\Test\Python\test_flask> nosetests-3.4.exe
E
====================================================================== …
Run Code Online (Sandbox Code Playgroud)

python flask flask-sqlalchemy flask-testing

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

如果用户定义的构造函数省略了数据成员的初始化,会发生什么?

假设:

class Foo
{
public: 
    Foo();
    Foo(typeA a);
private:
    typeB b;
    typeC c;
};

Foo::Foo(typeA a)
{
    //do something with a, but nothing with b nor c.
}

int main()
{
    typeA a;
    Foo foo(a);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这会发生什么?是foo.bfoo.c默认初始化?以这种方式定义构造函数是一个好习惯吗?

c++ c++11

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