有什么区别
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)
应该首选哪一个?
如果我正确理解了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?
我了解到以这种方式使用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++ 11标准中,它指出(参见cppreference.com,另见标准的第20.4.2.4节)它指出
template< class... Types >
tuple<VTypes...> make_tuple( Types&&... args );
Run Code Online (Sandbox Code Playgroud)
创建一个元组对象,从参数类型中推导出目标类型.
对于每一个
Ti
在Types...
时,相应的类型Vi
中Vtypes...
是std::decay<Ti>::type
除非的应用std::decay
在结果中std::reference_wrapper<X>
为某种类型的X
,在这种情况下,推定的类型是X&
.
我想知道:为什么参考包装器在这里处理特殊?
比方说,我有两个线程A
并B
写入一个全局布尔变量fA
,fB
分别最初设置为false
受std::mutex
对象保护mA
并mB
分别受以下对象保护:
// 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
在不同的线程不同的顺序C
和D
?换句话说,可以是以下程序
#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) 代码段如下.无法理解为什么我收到此错误.
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) 我想读写PI作为3,141592
代替3.141592
,如使用逗号是许多欧洲国家普遍.我怎样才能用iostream
s 实现这个目标?换一种说法
cout << 3.141592;
Run Code Online (Sandbox Code Playgroud)
应该打印
3,141592
Run Code Online (Sandbox Code Playgroud)
到标准输出.
编写代码就像
struct S
{
this() // compile-time error
{
}
}
Run Code Online (Sandbox Code Playgroud)
给我一个错误信息说
default constructor for structs only allowed with @disable and no body.
为什么??
是调用者还是被调用者复制或移动函数的返回值?例如,如果我想实现队列的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)
现在是非常安全的吗?
我想写一个模板函数,如下所示:
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
才能使用.我想要的是一个模板函数,其中所有参数类型都相同.有一个简单的方法吗?