如果元素std::initializer_list总是const值,为什么我们有模板方法喜欢begin()/end()和不cbegin()/cend()?这个名称(通过惯例,与例如比较std::vector)可以表明,当它们总是返回时,两种std::initializer_list方法都可以iterator返回const_iterator.
根据"Five of Rule",当我声明其中一个:复制或移动操作或析构函数时,我必须编写所有这些,因为编译器不会为我生成它们(其中一些).但是如果我的class(A)派生自带有虚拟析构函数的抽象类,这是否意味着类中的析构函数A将被视为"用户定义"?因此,移动语义不适用于此类的对象,A因为编译器不会为我生成移动构造函数?
struct Interface {
    virtual void myFunction() = 0;
    virtual ~Interface() {};
};
struct A : public Interface {
    void myFunction() override {};
};
我想将所有struct成员初始化为0.常见的解决方案是做这样的事情:
struct foo bar = {0}
我创建这个例子:
#include <stdio.h>
struct Stru2 {
    int c;
    int d;
};
struct Stru1 {
    int a;
    Stru2 b;
};
int main()
{
    struct Stru1 aaa = { 0 };
    return aaa.b.c;
}
我用这个参数编译(gcc 4.6.3),以确保ANSI如何处理它
gcc -Wall -Wextra -pedantic -ansi main.cpp
我收到以下警告:
main.cpp: In function ‘int main()’: 
main.cpp:36:28: warning: missing initializer for member ‘Stru1::b’ [-Wmissing-field-initializers]
问题是,为什么-Wextra会产生这个警告?也许并不总是"= {0}",将所有成员设置为0?
在GCC中是否有任何标志(如-Wempty-body在clang中),这可以帮助我在while/for循环的大括号后检测分号?有时人类很难找到这些简单的错误.
int i = 0;
for (i = 0; i < 10; ++i);
{
    cout << i << endl;
}
我使用GCC 4.7.3和clang 3.2-1~exp9ubuntu1.
编辑:我还检查编译器是否可以帮助我在"if-else语句"之后发现这些错误.
if (i == 0)
{
    cout << i << endl;
}
else;
{
    cout << i << endl;
}
什么是有趣的gcc比clang更有用(-Wall -pedantic -Wempty-body通过打印警告使用此flags():
main.cpp:30:9: warning: suggest braces around empty body in an ‘else’ statement [-Wempty-body]
在 python 中是否有任何等效的严格模拟?一些机制来报告模拟方法的意外调用(在这个例子中是 action.step2()),就像在 GoogleMock 框架中一样。
class Action:
    def step1(self, arg):
        return False
    def step2(self, arg):
        return False
def algorithm(action):
    action.step1('111')
    action.step2('222')
    return True
class TestAlgorithm(unittest.TestCase):
    def test_algorithm(self):
        actionMock = mock.create_autospec(Action)
        self.assertTrue(algorithm(actionMock))
        actionMock.step1.assert_called_once_with('111')
最近我看到了几个这样的代码示例,其中std :: move用于构造函数初始化列表(不是移动构造函数).
class A {
public:
    A(std::shared_ptr<Res> res) : myRes(std::move(res)) {
    // ...
    }
private:
    std::shared_ptr<Res> myRes;
}
我得到的信息是这个结构是出于优化原因.我个人使用std :: move尽可能少见.我威胁他们作为演员(如Scott Meyers所说),并且只在调用者代码中(只有例外是移动构造函数).对我来说,它看起来像某种混淆或微观优化,但也许我错了.是不是真的,如果没有std :: move,编译器不会产生更快的代码?
c++ ×5
c++11 ×3
c ×2
gcc ×2
ansi ×1
clang ×1
iterator ×1
mocking ×1
move ×1
optimization ×1
python ×1
python-mock ×1
unit-testing ×1