小编Mar*_*ica的帖子

声明类型包含未扩展的参数包“Args”

我正在尝试编写一个侦听器/回调类,但我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)

有什么建议么?谢谢!

c++ c++11

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

在函数返回的临时对象上调用成员函数是否安全(函数返回后)?

当临时对象由(另一个)函数创建然后返回时,我不知道调用临时对象的成员函数是否安全.

以下是一个例子:

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)

c++

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

如何转发构造函数中所有未知参数来初始化成员对象?

我正在尝试做这样的事情:

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的构造函数。

我怎样才能做到呢?

c++ parameter-passing

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

什么时候std :: unordered_map :: insert会失败?

我注意到insert函数std::unordered_map返回a std::pair.

std::pair显示值是否真正插入的第二个元素.但是,我对此感到困惑.可以std::unordered_map通过哈希映射实现的a在插入时失败吗?什么时候会发生?

这是cppreference的描述:

返回值
1-2)返回一个由插入元素的迭代器(或阻止插入的元素)组成的对,以及表示插入是否发生的bool.

c++ unordered-map insert c++11

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

为什么在构造函数中发出信号不起作用?

我想在构造函数中发出信号,如下所示:

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)

我不知道为什么.

c++ qt

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

标签 统计

c++ ×5

c++11 ×2

insert ×1

parameter-passing ×1

qt ×1

unordered-map ×1