我正在尝试编写一个侦听器/回调类,但我get Declaration type contains unexpanded parameter pack 'Args'在调用方法中。我想这样称呼:
listeners.call (&ListenerType::myCallbackMethod, arg1, arg2, etc);
好的,我可以创建许多具有不同数量参数的调用方法,但如果我可以只做一种方法更好
template <typename ...Args>
void call (void (ListenerClass::*callbackFunction) (Args), Args && ...value) // Compiler error: Declaration type contains unexpanded parameter pack 'Args'
{
auto iter = listeners.begin();
while (iter != listeners.end())
{
if(auto p = iter->lock())
{
(p->*callbackFunction) (std::forward<Args>(value)...);
++iter;
}
else
iter = listeners.erase(iter);
}
};
Run Code Online (Sandbox Code Playgroud)
有什么建议么?谢谢!
当临时对象由(另一个)函数创建然后返回时,我不知道调用临时对象的成员函数是否安全.
以下是一个例子:
string getString(const string &str)
{
string tempStr = str + str;
return tempStr;
}
int main()
{
if(getString("aaaaa").compare("bbbb") == 0) { // is it safe?
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
我也做了如下的终身测试.
class Foo
{
public:
Foo()
{
cout << __PRETTY_FUNCTION__ << " :" << this << endl;
}
~Foo()
{
cout << __PRETTY_FUNCTION__ << " :" << this << endl;
}
void show()
{
cout << this << endl;
}
};
Foo func(void)
{
return Foo();
}
int …Run Code Online (Sandbox Code Playgroud) 我正在尝试做这样的事情:
struct Foo {
int _val;
Foo(int v) : _val(v){}
};
struct Bar {
const std::string &_name;
Bar(const std::string &name) : _name(name) {}
};
template<typename T>
struct Universal {
T _t;
Universal(...) : _t(...) {}
};
// I want to use Universal for Foo abd Bar in the same way:
Universal<Foo> UF(9); // 9 is for Foo
Universal<Bar> UB("hello"); // "hello" is for bar
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我想将Universal的构造函数中的所有参数转发给T的构造函数。
我怎样才能做到呢?
我注意到insert函数std::unordered_map返回a std::pair.
std::pair显示值是否真正插入的第二个元素.但是,我对此感到困惑.可以std::unordered_map通过哈希映射实现的a在插入时失败吗?什么时候会发生?
这是cppreference的描述:
返回值
1-2)返回一个由插入元素的迭代器(或阻止插入的元素)组成的对,以及表示插入是否发生的bool.
我想在构造函数中发出信号,如下所示:
VideoStream::VideoStream(QWidget *parent):
QMainWindow(parent),
ui(new Ui::VideoStream)
{
ui->setupUi(this);
…… //m_deviceIP already intialized here
emit streamReq(m_deviceIP);//emitting at here
recentRecordReq();//this function include a emit sentence,too
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用!为了测试,我添加一个Button然后将emit句子移动到插槽中,它运行良好:
void VideoStream::on_streamReqBtn_clicked()
{
emit streamReq(m_deviceIP);
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么.