#include <iostream>
struct A
{
A() { std::cout << "(A::A)"; }
};
struct B
{
B() { std::cout << "(B::B)"; }
};
struct C
{
template<typename ...Args>
C(Args && ...) {}
};
int main(int agrc, char *argv[])
{
C {A(), B()}; // <-- prints (B::B)(A::A)
std::cout << std::endl;
C {(A(), B())}; // <-- prints (A::A)(B::B)
std::cout << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我有两个问题:
编辑:我用msvs 2013编译了它
我有一个函数at旨在通过运行时指定的索引访问std :: tuple元素
template<std::size_t _Index = 0, typename _Tuple, typename _Function>
inline typename std::enable_if<_Index == std::tuple_size<_Tuple>::value, void>::type
for_each(_Tuple &, _Function)
{}
template<std::size_t _Index = 0, typename _Tuple, typename _Function>
inline typename std::enable_if < _Index < std::tuple_size<_Tuple>::value, void>::type
for_each(_Tuple &t, _Function f)
{
f(std::get<_Index>(t));
for_each<_Index + 1, _Tuple, _Function>(t, f);
}
namespace detail { namespace at {
template < typename _Function >
struct helper
{
inline helper(size_t index_, _Function f_) : index(index_), f(f_), count(0) {}
template < typename _Arg …Run Code Online (Sandbox Code Playgroud) #include <iostream>
struct A
{
void operator()(const char *)
{
std::cout << "void operator()(const char *)" << std::endl;
}
};
int main(int argc, char* argv[])
{
A a;
a{"hi"};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
msvs12对这段代码感到满意,我无法理解为什么.是bug还是其他什么?
更新:我尝试使用msvs 2013(v12.0.31101.0 Update 4)
我正在实现一个旨在执行远程进程中的任务的RPC系统.RPC系统的一个节点是Monitor,它应该记录每个调用.
template<typename Transport, typename Journal>
class Monitor
{
public:
Monitor(Transport transport, Journal &journal) :
transport{std::move(transport)},
journal{journal}
{
}
public:
template<typename Method>
typename Method::Result operator()(const Method &method)
{
Method::Result result;
journal("->", Method::Name());
result = transport(method);
journal("<-", Method::Name());
return result;
}
private:
Transport transport;
Journal &journal;
};
Run Code Online (Sandbox Code Playgroud)
除了Method :: Result为void之外的一种情况,它工作正常.为了解决这个问题,我不得不将operator()分成两部分
template<typename Transport, typename Journal>
template<typename Method>
std::enable_if_t<std::is_same<typename Method::Result, void>::value, typename Method::Result> operator()(const Method &method)
{
journal("->", Method::Name());
transport(method);
journal("<-", Method::Name());
}
template<typename Transport, typename Journal>
template<typename Method>
std::enable_if_t<!std::is_same<typename Method::Result, void>::value, typename Method::Result> …Run Code Online (Sandbox Code Playgroud) 在一些教程中,我看到了这样的自旋锁实现
class spin_lock
{
atomic<unsigned int> m_spin ;
public:
spin_lock(): m_spin(0) {}
~spin_lock() { assert( m_spin.load(memory_order_relaxed) == 0);}
void lock()
{
unsigned int nCur;
do { nCur = 0; }
while ( !m_spin.compare_exchange_weak( nCur, 1, memory_order_acquire ));
}
void unlock()
{
m_spin.store( 0, memory_order_release );
}
};
Run Code Online (Sandbox Code Playgroud)
我们真的需要memory_order_acquire/ memory_order_release标签compare_exchange_weak和store操作吗?或者memory_order_relaxed在这种情况下是否足够,因为没有Synchronizes-With关系?
更新:谢谢您的解释!我正在考虑spin_lock,没有使用它的上下文.
请看样品
std::atomic < std::shared_ptr < int > > a;
std::shared_ptr < int > b;
std::shared_ptr < int > c = std::make_shared < int > (10);
while(a.compare_exchange_weak(b, c));
assert(a.load() == c);
assert(a.load().use_count() == 2); // <- assertion is failed.
Run Code Online (Sandbox Code Playgroud)
你怎么看?是编译器错误吗?
在win32模式下使用msvs 2013构建
我有Decimal类,它实现了十进制浮点运算.它可以用整数算术类型或作为字符串存储的小数位初始化.由于其近似性质,故意避免内置浮点类型.这种方法效果很好,但编码为字符串的小数位看起来很尴尬.是否有可能以某种方式使用C++文字在场景后面构建字符串(或二进制表示)?
Decimal a{"1.254684987"}; // current initialization
Decimal b{1.254684987_dec}; // desired way
Run Code Online (Sandbox Code Playgroud)