如何创建递归可变参数模板以打印出参数包的内容?我正在尝试这个,但它无法编译:
template <typename First, typename ...Args>
std::string type_name () {
return std::string(typeid(First).name()) + " " + type_name<Args...>();
}
std::string type_name () {
return "";
}
Run Code Online (Sandbox Code Playgroud)
我该如何结束递归?
请考虑以下代码:
void ListenerImpl::attach(boost::shared_ptr<ISubscriber> subscriber)
{
boost::unique_lock<boost::mutex>(mtx);
subscribers.push_back(subscriber);
}
void ListenerImpl::notify(MsgPtr msg)
{
boost::unique_lock<boost::mutex>(mtx);
//notify all subscribers
BOOST_FOREACH(boost::shared_ptr<ISubscriber> subscriber, subscribers){
subscriber->update(msg);
}
}
Run Code Online (Sandbox Code Playgroud)
(这是GoF中描述的观察者模式的实现.)这里的用户干预是保护attach()和notify()不同时运行,因此boost :: unique_lock.目标是保护subscribers容器.
但确实很难注意到锁实际上只是暂时的(仔细看,没有为它们指定名称).因此,当临时被破坏时,互斥锁上的锁定将被立即释放,即代码不是线程安全的.我希望在这种情况下编译器警告.像"未使用的临时"之类的东西.
更糟糕的是,cppcheck也不会认识到这个错误.(cppcheck:ac/c ++代码分析工具http://sourceforge.net/apps/mediawiki/cppcheck/index.php?title=Main_Page)
Gcc对未使用的变量发出警告.这里的临时变量是一个未使用的变量,绝对是程序员注意力不集中的结果.那么,为什么在这种情况下没有警告呢?发现这种情况可能太复杂了吗?
使用有什么好处boost::any_range?这是一个例子:
typedef boost::any_range<
int
, boost::forward_traversal_tag
, int
, std::ptrdiff_t
> integer_range;
void display_integers(const integer_range& rng)
{
boost::copy(rng,
std::ostream_iterator<int>(std::cout, ","));
std::cout << std::endl;
}
int main(){
std::vector<int> input{ ... };
std::list<int> input2{ ... };
display_integers(input);
display_integers(input2);
}
Run Code Online (Sandbox Code Playgroud)
但是使用模板参数可以实现具有更高效率的相同功能,该参数满足ForwardRange概念:
template <class ForwardRange>
void display_integers(const ForwardRange& rng)
{
boost::copy(rng,
std::ostream_iterator<int>(std::cout, ","));
std::cout << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
所以我在寻找使用any_range值得的场景.也许我错过了什么.
在boost::interprocess文档中,据说要将容器存储在共享内存中:
operator==()在运行时进行测试.allocator::pointer,容器可能不会假定allocator::pointer是原始指针.allocator::construct和allocator::destroy函数构造所有对象.我正在使用gcc 4.7.1和-std = c ++ 11(和boost 1.53).使用下面定义的ShmVector类型是否安全?
typedef boost::interprocess::allocator<int,
boost::interprocess::managed_shared_memory::segment_manager> ShmemAllocator;
typedef std::vector<int, ShmemAllocator> ShmVector;
Run Code Online (Sandbox Code Playgroud)
我尝试了一个使用这种类型的虚拟进程,看起来它正在工作,但我仍然不确定gcc4.7.1中的向量是否满足所有要求.我对第一个要求特别不确定.
#include <iostream>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <vector>
#include <cstdlib> //std::system
typedef boost::interprocess::allocator<int,
boost::interprocess::managed_shared_memory::segment_manager> ShmemAllocator;
typedef std::vector<int, ShmemAllocator> ShmVector;
int main(int argc, char *argv[])
{
if(argc == 1){ //Parent process
struct shm_remove
{
shm_remove() { boost::interprocess::shared_memory_object::remove("MySharedMemory"); }
~shm_remove(){ boost::interprocess::shared_memory_object::remove("MySharedMemory"); }
} remover;
//Create a new …Run Code Online (Sandbox Code Playgroud) 我从新标准std::shared_lock模板类中非常缺少.在Boost.Thread中boost::shared_lock,甚至boost::upgrade_lock存在.
为什么这样,std::unique_lock在C++ 11中没有std :: shared_lock ?
如何获得类似的行为boost::shared_lock,但在纯C++ 11中呢?
我正在考虑使用boost::shared_lock<std::mutex>,但这没有那么多意义,因为std::mutex没有lock_shared()成员.而且,没有这样的std::shared_mutex.
可以轻松转到当前缓冲区中的最后一个编辑位置.请参阅如何返回Vim中最后一行之前编辑的行? changelist是缓冲本地的,每个缓冲区都有自己的更改列表.然而,我从最近编辑的缓冲区导航到另一个缓冲区是很常见的,以某种方式回到原始缓冲区中的最后一个编辑位置会很好.有没有办法回到最后一次插入或修改发生的地方?
我想创建一个条件,仅当具有指定 glob 的文件名存在时,该条件才为 true。像这样(它不是工作cmake代码,只是为了表达我的意图):
if(EXISTS "${LIBRARY_PATH}/lib*.a")
...
elseif(EXISTS "${LIBRARY_PATH}/lib/*.dll")
...
Run Code Online (Sandbox Code Playgroud) 来自C++ Concurrency in Action:
std :: atomic和std :: atomic_flag之间的区别是std :: atomic可能不是无锁的; 实现可能必须在内部获取互斥锁,以确保操作的原子性
我想知道为什么.如果保证atomic_flag是无锁的,为什么它也不能保证atomic<bool>?这是因为会员功能compare_exchange_weak吗?我知道有些机器缺少单一的比较和交换指令,原因是什么?
我有预建的第三方库(Boost),我想链接到我的目标.所有这些都存储在一个目录下,如$ {BOOST_PATH} /lib/libboost_thread.a,$,BOOST_PATH} /lib/libboost_log.a等.所以我想做这样的事情:
target_link_libraries(${TARGET} PRIVATE "${BOOST_PATH}/libboost*.a")
我读过那个文件( GLOB ......)可能会被使用,但强烈反对.我不确定它会起作用.为什么?如果无法更改Boost库的目录结构,您将如何解决此问题?
是否可以将 vim 与基于缓冲区的 {expandtab、tabstop、shiftwidth} 设置一起使用?例如,我编辑一个使用 2 个空格缩进的文件
stuct A {
void foo () {
int x;
}
};
Run Code Online (Sandbox Code Playgroud)
以及另一个带有选项卡的文件
int main() {
int i; // \t is used as indentation
return i;
}
Run Code Online (Sandbox Code Playgroud)
当然,我可以逐个缓冲区手动设置值,但我希望 vim 自动执行此操作。这可以做到吗?或者有一个插件可以为我做到这一点吗?
我在SNMPV2实现中使用boost :: asio :: ip :: udp :: resolver来确定主机是否可访问.
using Resolver = boost::asio::ip::udp::resolver;
Resolver resolver(ioService);
Resolver::query query(connectOptions.getHost(),
connectOptions.getPort());
Resolver::iterator endpointIterator;
BOOST_LOG_SEV(logger, Severity::debug) << "Waiting for async resolve";
endpointIterator = resolver.async_resolve(query, yield);
BOOST_LOG_SEV(logger, Severity::debug) << "Async resolve done";
if (endpointIterator == Resolver::iterator{}) { // unreachable host
using namespace boost::system;
throw system_error{error_code{SnmpWrapperError::BadHostname}};
}
Run Code Online (Sandbox Code Playgroud)
我有一个测试用例,我测试当一个不存在的主机名和一个exitent主机名被查询时发生的事情并行:
2013-09-16 10:45:28.687001: [DEBUG ] 0x88baf8 SnmpConnection: connect
2013-09-16 10:45:28.687396: [DEBUG ] 0x88baf8 SnmpConnection: host: non_existent_host_name_
2013-09-16 10:45:28.687434: [DEBUG ] 0x88baf8 SnmpConnection: port: 1611
2013-09-16 10:45:28.687456: [DEBUG ] 0x88baf8 …Run Code Online (Sandbox Code Playgroud) c++ ×5
c++11 ×3
boost ×2
cmake ×2
glob ×2
vim ×2
atomic ×1
boost-asio ×1
boost-range ×1
boost-thread ×1
gcc-warning ×1
gcc4.7 ×1
mutex ×1
recursion ×1
stdatomic ×1
stl ×1
temporaries ×1
type-erasure ×1
typeid ×1
vector ×1