小编Ral*_*zky的帖子

模板函数与带有自动参数的命名lambda

有什么区别

template <typename T> void func( T t ) { /* ... */ }
Run Code Online (Sandbox Code Playgroud)

和使用带有自动参数的lambdas的C++ 14替代方案?

auto func = []( auto t ) { /* ... */ }
Run Code Online (Sandbox Code Playgroud)

应该首选哪一个?

c++ lambda templates generic-lambda c++14

11
推荐指数
2
解决办法
1186
查看次数

如何在没有竞争条件的情况下将QFutureWatcher与QtConcurrent :: run()一起使用

如果我正确理解了QFutureWatcher文档中的以下代码,那么最后一行之间存在竞争条件:

// Instantiate the objects and connect to the finished signal.
MyClass myObject;
QFutureWatcher<int> watcher;
connect(&watcher, SIGNAL(finished()), &myObject, SLOT(handleFinished()));

// Start the computation.
QFuture<int> future = QtConcurrent::run(...);
watcher.setFuture(future);
Run Code Online (Sandbox Code Playgroud)

如果函数...QtConcurrent::run(...)下一行之前完成被调用,那么watcher.finished()信号将永远不会被触发.我的假设是否正确?我该如何解决这个bug?

c++ concurrency qt future

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

`auto`的ref-和cv-stripping属性.

我了解到以这种方式使用auto声明变量

auto var = expr;
Run Code Online (Sandbox Code Playgroud)

基本上就是从它的类型expr和剥离&/ && - 引用和所有顶级constness和volatile.这是否意味着上述行完全等同于以下内容?

std::remove_cv<std::remove_ref<decltype(expr)>::type>::type var = expr;
Run Code Online (Sandbox Code Playgroud)

c++ decltype auto c++11

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

为什么std :: make_tuple将std :: reference_wrapper <X>参数转换为X&?

在C++ 11标准中,它指出(参见cppreference.com,另见标准的第20.4.2.4节)它指出

template< class... Types >
tuple<VTypes...> make_tuple( Types&&... args );
Run Code Online (Sandbox Code Playgroud)

创建一个元组对象,从参数类型中推导出目标类型.

对于每一个TiTypes...时,相应的类型ViVtypes...std::decay<Ti>::type除非的应用std::decay在结果中std::reference_wrapper<X>为某种类型的X,在这种情况下,推定的类型是X&.

我想知道:为什么参考包装器在这里处理特殊?

c++ tuples c++11 reference-wrapper

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

std :: mutex是否顺序一致?

比方说,我有两个线程AB写入一个全局布尔变量fA,fB分别最初设置为falsestd::mutex对象保护mAmB分别受以下对象保护:

// Thread A
mA.lock();
assert( fA == false );
fA = true;
mA.unlock();

// Thread B
mB.lock()
assert( fB == false );
fB = true;
mB.unlock()
Run Code Online (Sandbox Code Playgroud)

是否可以观察修改fA,并fB在不同的线程不同的顺序CD?换句话说,可以是以下程序

#include <atomic>
#include <cassert>
#include <iostream>
#include <mutex>
#include <thread>
using namespace std;

mutex mA, mB, coutMutex;
bool fA = false, fB = false;

int main()
{
    thread …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading mutex memory-barriers c++11

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

c ++中的函数指针:错误:必须使用'.*'或' - >*'来调用函数中的指向成员函数

代码段如下.无法理解为什么我收到此错误.

void
SipObj::check_each_field()
{
map <std::string, sip_field_getter>::iterator msg;
string str;
char name[20];
bool res = false;
sscanf(get_payload(), "%s %*s", name);
LOGINFO(lc()) <<  "INVITE:" << name;
str = name;
msg = sip_field_map.find(str);
if (msg != sip_field_map.end()) {
    sip_field_getter sip_field = msg->second;
    res = (this).*sip_field();
}
}

 typedef bool (SipObj::*sip_field_getter)();
 static map <std::string, sip_field_getter> sip_field_map;

sip_field_getter is a place holder for function names
Run Code Online (Sandbox Code Playgroud)

c++ function-pointers

9
推荐指数
3
解决办法
7506
查看次数

如何将小数点分隔符设置为逗号?

我想读写PI作为3,141592代替3.141592,如使用逗号是许多欧洲国家普遍.我怎样才能用iostreams 实现这个目标?换一种说法

cout << 3.141592;
Run Code Online (Sandbox Code Playgroud)

应该打印

3,141592
Run Code Online (Sandbox Code Playgroud)

到标准输出.

c++ formatting iostream decimal-point comma

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

为什么我不能在D中为结构体实现默认构造函数?

编写代码就像

struct S
{
    this() // compile-time error
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

给我一个错误信息说

default constructor for structs only allowed with @disable and no body.

为什么??

d default-constructor

9
推荐指数
2
解决办法
1002
查看次数

谁复制了函数的返回值?

是调用者还是被调用者复制或移动函数的返回值?例如,如果我想实现队列的pop()函数,就像这样

template <typename T> 
class queue
{
    std::deque<T> d;
public:
    // ... //
    T pop()
    {
        // Creates a variable whose destructor removes the first
        // element of the queue if no exception is thrown. 
        auto guard = ScopeSuccessGuard( [=]{ d.pop_front(); } );
        return d.front();
    }
}
Run Code Online (Sandbox Code Playgroud)

是复制前面元素后调用的范围内容的析构函数?

编辑:后续问题:行

auto item = q.pop();
Run Code Online (Sandbox Code Playgroud)

现在是非常安全的吗?

c++ copying scopeguard c++11

8
推荐指数
2
解决办法
364
查看次数

具有相等参数类型的可变参数模板函数

我想写一个模板函数,如下所示:

template <typename T>
void f( const T & ...args ) // <-- This doesn't work, unfortunately.
{
    std::array<T> arr = { args... };
    // and so forth.
}
Run Code Online (Sandbox Code Playgroud)

显然,C++不允许这样做,因为左侧需要一个模板参数包...args才能使用.我想要的是一个模板函数,其中所有参数类型都相同.有一个简单的方法吗?

c++ variadic-templates c++11

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