小编Jur*_*aho的帖子

拉伸元素以包含所有孩子

如何在QML中自动拉伸元素以使其所有子元素都适合它?以及如何指定间距?例如,我想在文本周围有一个矩形.矩形应该有一些内部间距.

如果我写下面的内容,那么矩形的大小为0,0.

Rectangle {
    color: "gray"
    anchors.centerIn: parent;

    Text {
        text: "Hello"
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我尝试使用Column元素修复它,如如何使QML项目增长以适应内容?,然后我通过整个窗口/父级得到一个列,

Column {
    anchors.centerIn: parent

    Rectangle {
        color: "gray"
        anchors.fill: parent
    }

    Text {
        anchors.centerIn: parent
        text: "Hello"
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

我也尝试使用Flow元素代替Column,但后来我在整个窗口/父级中获得了一行.

qt qml

23
推荐指数
2
解决办法
2万
查看次数

从Lua调用函数时如何处理C++异常?

我有一个工作的C++函数,我可以从Lua调用.为了证明我的问题,这里有一个例子:

int PushHello(lua_State *L){
    string str("Hello");
    lua_pushlstring(L, str.data(), str.length());
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

注意:我知道我不必在那里使用字符串变量,但它是为了证明这个问题.

这是我的两个问题:

  1. 当我从Lua字符串构造函数调用此函数时可能会抛出异常.那是问题吗?Lua会处理它并正确解开Lua堆栈吗?我不这么认为.我怎么解决这个问题?我是否需要添加try/catch所有此类代码并将异常转换为lua_error?是不是有更好的解决方案?

  2. 我可能通过将Lua编译为C++来解决的另一个问题是,如果使用longjmp,则lua_pushlstring()调用lua_error()字符串析构函数时不会调用它.通过编译为C++并抛出异常而不是使用longjmp来解决问题吗?

为了澄清,我可以看到问题1的可能解决方案是:

int PushHello(lua_State *L){
    string str;
    try{
        str.assign("Hello");
    catch(exception &e){
        luaL_error(L, e.what());
    }
    lua_pushlstring(L, str.data(), str.length());
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

但这非常难看并且容易出错,因为try/catch需要添加到许多地方.它可以作为一个宏来完成并放置每个可以抛出的命令,但这不会更好.

c++ lua exception

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

为什么不能在lambda中使用私有方法?

有这样的课程:

class A {
public:
    bool hasGrandChild() const;

private:
    bool hasChild() const;
    vector<A> children_;
};
Run Code Online (Sandbox Code Playgroud)

为什么不能hasChild()在这样的方法中定义的lambda表达式中使用私有方法hasGrandChild()

bool A::hasGrandChild() const {
    return any_of(children_.begin(), children_.end(), [](A const &a) {
        return a.hasChild();
    });
}
Run Code Online (Sandbox Code Playgroud)

编译器发出该方法hasChild()在上下文中是私有的错误.有没有解决方法?

编辑: 似乎我发布它的代码最初有效.我认为它是等价的,但是在GCC上不起作用的代码更像是这样:

#include <vector>
#include <algorithm>

class Foo;

class BaseA {
protected:
    bool hasChild() const { return !children_.empty(); }
    std::vector<Foo> children_;
};

class BaseB {
protected:
    bool hasChild() const { return false; }
};

class Foo : public BaseA, public …
Run Code Online (Sandbox Code Playgroud)

c++ lambda c++11

20
推荐指数
2
解决办法
6629
查看次数

通过函数安全传递引用?

以下函数在C++ 03或C++ 11中是安全的还是展示了UB?

string const &min(string const &a, string const &b) {
    return a < b ? a : b;
}

int main() {
    cout << min("A", "B");
}
Run Code Online (Sandbox Code Playgroud)
  • 可以通过引用返回对传递给函数的对象的引用吗?

  • 是否保证临时string对象不会过早销毁?

  • 给定的函数是否有 min可能展示UB(如果它不在给定的上下文中)?

  • 是否有可能在避免复制或移动的同时提供同等但安全的功能?

c++ c++11

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

C++模板函数的行为

假设我有这个功能:

bool f(int&& one, int&& two) { }
Run Code Online (Sandbox Code Playgroud)

如果我尝试使用此代码调用它:

int x = 4;
f(x, 5);
Run Code Online (Sandbox Code Playgroud)

编译器会抱怨它不能将x从左值引用转换为右值引用,这是正确的.

现在,如果我将f转换为模板函数,如下所示:

template <class T, class U>
bool f(T&& one, U&& two) { }
Run Code Online (Sandbox Code Playgroud)

然后我可以用左值引用来调用它:

int x = 5;
f(x, 5);
Run Code Online (Sandbox Code Playgroud)

为什么会这样?为什么编译器在这种情况下不抱怨?

c++ templates c++11 universal-reference

18
推荐指数
3
解决办法
381
查看次数

将bind1st用于通过引用获取参数的方法

我有这样的结构:

struct A {
    void i(int i) {}
    void s(string const &s) {}
};
Run Code Online (Sandbox Code Playgroud)

现在当我尝试这个:

bind1st(mem_fun(&A::i), &a)(0);
bind1st(mem_fun(&A::s), &a)("");
Run Code Online (Sandbox Code Playgroud)

第一行编译好,但第二行生成错误:

c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional(299): error C2535: 'void std::binder1st<_Fn2>::operator ()(const std::basic_string<_Elem,_Traits,_Ax> &) const' : member function already defined or declared
          with
          [
              _Fn2=std::mem_fun1_t<void,A,const std::string &>,
              _Elem=char,
              _Traits=std::char_traits<char>,
              _Ax=std::allocator<char>
          ]
          c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional(293) : see declaration of 'std::binder1st<_Fn2>::operator ()'
          with
          [
              _Fn2=std::mem_fun1_t<void,A,const std::string &>
          ]
          c:\work\sources\exception\test\exception\main.cpp(33) : see reference to class template instantiation 'std::binder1st<_Fn2>' being compiled
          with …
Run Code Online (Sandbox Code Playgroud)

c++ visual-c++

15
推荐指数
2
解决办法
2811
查看次数

从函数返回stl容器

从函数返回stl容器的最佳方法(性能方面)是什么?返回的容器通常包含数千个项目.

方法1:

typedef std::list<Item> ItemContainer;

ItemContainer CreateManyItems() {
    ItemContainer result;

    // fill the 'result' ...

    return result;
}

ItemContainer a = CreateManyItems();
Run Code Online (Sandbox Code Playgroud)

方法2:

void CreateManyItems(ItemContainer &output) {
    ItemContainer result;

    // fill the 'result' ...

    output.swap(result);
} 

ItemContainer a;
CreateManyItems(a);
Run Code Online (Sandbox Code Playgroud)

方法3:

void std::auto_ptr<ItemContainer> CreateManyItems() {
    std::auto_ptr<ItemContainer> result(new ItemContainer);

    // fill the 'result' ...

    return result;
}

std::auto_ptr<ItemContainer> a = CreateManyItems();
Run Code Online (Sandbox Code Playgroud)

还是有更好的方法吗?

c++ containers stl

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

循环范围内的初始化列表

我有从单个超类型派生的不同类型的对象.我想知道std::initializer在循环范围内使用list 是否有任何缺点,如下所示:

for(auto object: std::initializer_list<Object *>{object1, object2, object3}) {
}
Run Code Online (Sandbox Code Playgroud)

它是完全正常和有效还是使用阵列会更好?对我来说,std::array解决方案似乎对编译器更具限制性,并且存在明确说明大小的缺点:

for(auto object: std::array<Object*, 3>{object1, object2, object3}) {
}
Run Code Online (Sandbox Code Playgroud)

是否有任何其他或更好的方法迭代明确给定的对象列表?

c++ for-loop range initializer-list c++11

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

避免整数乘法溢出,然后除法

我有两个积分变量ab和恒定sRESP.d.我需要计算(a*b)>>sresp 的值.a*b/d.问题是乘法可能会溢出,最终结果将不正确,即使它a*b/d可以适合给定的整数类型.

怎么能有效地解决?直接的解决方案是扩展变量ab更大的整数类型,但可能没有更大的整数类型.有没有更好的方法来解决这个问题?

c c++ algorithm numbers

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

静态变量的地址

我想尝试一个简单的类来进行唯一的ID转换.我正在考虑添加一个静态方法:

class A {
  static int const *GetId() {
    static int const id;
    return &id;
  }
};
Run Code Online (Sandbox Code Playgroud)

然后每个类将通过唯一标识int const *.这保证有效吗?返回的指针真的是唯一的吗?有没有更简单的解决方案?

我也想过指向std::type_info:

class A {
  static std::type_info const *GetId() {
    return &typeid(A);
  }
};
Run Code Online (Sandbox Code Playgroud)

那个更好吗?

编辑:

我不需要使用id进行序列化.我只想识别一小组基类,并希望某些类的所有子类具有相同的id

c++

12
推荐指数
3
解决办法
4633
查看次数