桃李不言,下自成蹊。
尽自己一份力,让c++的世界变得更美好!
C++开源社区:http://purecpp.org/
c++11基础库
桃李不言,下自成蹊。
尽自己一份力,让c++的世界变得更美好!
C++开源社区:http://purecpp.org/
您好,看了您的书依然对于std::move这部分不是很理解
在SyncQueue.hpp 第34行 list = std::move(m_queue)
之后对于m_queue没有任何操作,我认为这样做,之后的线程再对m_queue进行的操作都会导致错误,因为这时的m_queue已经变成了 'list' 交给工作线程去处理了,所以应该更新m_queue。。我还没想好应该怎样去更新,也不知道自己的想法是否正确。。希望您能解答我的疑问。
1-18的示例代码在哪里?太不仔细了吧。
abi::__cxa_demangle(typeid(TR).name()), nullptr,
nullptr, nullptr),
这里括号没对,应该是
abi::__cxa_demangle(typeid(TR).name() , nullptr,
nullptr, nullptr),
我想问如何知道Attach函数的输入函数F的所有形参类型?
Attach函数原型如下:
//注册消息
template
void Attach(F&& f, const string& strTopic="")
{
auto func = to_function(std::forward(f));
Add(strTopic, std::move(func));
}
因为我需要记录这些形参类型,在调用的时候首先尝试进行强制转换。
感谢回复
代码如下
std::unique_ptr<T, DeleterType> get()
{
if (pool_.empty())
{
throw std::logic_error("no more object");
}
//every time add custom deleter for default unique_ptr
std::unique_ptr<T, DeleterType> ptr(pool_.back().release(), [this](T* t)
{
pool_.push_back(std::unique_ptr<T>(t));
});
pool_.pop_back();
return std::move(ptr);
}
请教下,代码中使用的lambda表达式中这一句pool_.push_back这句的作用是什么呢?一直没看懂
你好!我在编译LinqCpp时出错了,修改了好多,现在只剩最后一个问题了,LinqCpp::where
的参数类型是const F &f
,test中给的实参是lambda表达式,用g++编译,报的错误是lambda closure没有提供赋值操作,但这里并没有赋值啊,还请指教。
下面是详细的编译信息,行号可能有不同,因为我修复了其他的编译错误。
In file included from /usr/include/boost/range/adaptors.hpp:15:0,
from LinqCpp.hpp:9,
from linq.cpp:7:
/usr/include/boost/range/adaptor/adjacent_filtered.hpp: In instantiation of ‘void boost::range_detail::skip_iterator<Iter, Pred, default_pass>::move_to_next_valid() [with Iter = boost::filter_iterator<TestLinqCpp()::<lambda(int)>, __gnu_cxx::__normal_iterator<const int*, std::vector<int> > >; Pred = boost::range_detail::unique_not_equal_to; bool default_pass = true]’:
/usr/include/boost/range/adaptor/adjacent_filtered.hpp:65:36: required from ‘boost::range_detail::skip_iterator<Iter, Pred, default_pass>::skip_iterator(boost::range_detail::skip_iterator<Iter, Pred, default_pass>::iter_t, boost::range_detail::skip_iterator<Iter, Pred, default_pass>::iter_t, const Pred&) [with Iter = boost::filter_iterator<TestLinqCpp()::<lambda(int)>, __gnu_cxx::__normal_iterator<const int*, std::vector<int> > >; Pred = boost::range_detail::unique_not_equal_to; bool default_pass = true; boost::range_detail::skip_iterator<Iter, Pred, default_pass>::iter_t = boost::filter_iterator<TestLinqCpp()::<lambda(int)>, __gnu_cxx::__normal_iterator<const int*, std::vector<int> > >]’
/usr/include/boost/range/adaptor/adjacent_filtered.hpp:143:68: required from ‘boost::range_detail::adjacent_filtered_range<P, R, default_pass>::adjacent_filtered_range(const P&, R&) [with P = boost::range_detail::unique_not_equal_to; R = boost::range_detail::filtered_range<TestLinqCpp()::<lambda(int)>, boost::iterator_range<__gnu_cxx::__normal_iterator<const int*, std::vector<int> > > >; bool default_pass = true]’
/usr/include/boost/range/adaptor/uniqued.hpp:40:50: required from ‘boost::range_detail::uniqued_range<ForwardRng>::uniqued_range(ForwardRng&) [with ForwardRng = boost::range_detail::filtered_range<TestLinqCpp()::<lambda(int)>, boost::iterator_range<__gnu_cxx::__normal_iterator<const int*, std::vector<int> > > >]’
/usr/include/boost/range/adaptor/uniqued.hpp:50:47: required from ‘boost::range_detail::uniqued_range<ForwardRng> boost::range_detail::operator|(ForwardRng&, boost::range_detail::unique_forwarder) [with ForwardRng = boost::range_detail::filtered_range<TestLinqCpp()::<lambda(int)>, boost::iterator_range<__gnu_cxx::__normal_iterator<const int*, std::vector<int> > > >]’
LinqCpp.hpp:101:50: required from ‘cosmos::LinqCpp<boost::range_detail::uniqued_range<ForwardRng> > cosmos::LinqCpp<R>::distinct() [with R = boost::range_detail::filtered_range<TestLinqCpp()::<lambda(int)>, boost::iterator_range<__gnu_cxx::__normal_iterator<const int*, std::vector<int> > > >]’
linq.cpp:24:64: required from here
/usr/include/boost/range/adaptor/adjacent_filtered.hpp:101:32: error: use of deleted function ‘boost::filter_iterator<TestLinqCpp()::<lambda(int)>, __gnu_cxx::__normal_iterator<const int*, std::vector<int> > >& boost::filter_iterator<TestLinqCpp()::<lambda(int)>, __gnu_cxx::__normal_iterator<const int*, std::vector<int> > >::operator=(const boost::filter_iterator<TestLinqCpp()::<lambda(int)>, __gnu_cxx::__normal_iterator<const int*, std::vector<int> > >&)’
it = m_last;
^
In file included from /usr/include/boost/range/adaptor/filtered.hpp:16:0,
from /usr/include/boost/range/adaptors.hpp:17,
from LinqCpp.hpp:9,
from linq.cpp:7:
/usr/include/boost/iterator/filter_iterator.hpp:44:9: note: ‘boost::filter_iterator<TestLinqCpp()::<lambda(int)>, __gnu_cxx::__normal_iterator<const int*, std::vector<int> > >& boost::filter_iterator<TestLinqCpp()::<lambda(int)>, __gnu_cxx::__normal_iterator<const int*, std::vector<int> > >::operator=(const boost::filter_iterator<TestLinqCpp()::<lambda(int)>, __gnu_cxx::__normal_iterator<const int*, std::vector<int> > >&)’ is implicitly deleted because the default definition would be ill-formed:
class filter_iterator
^
/usr/include/boost/iterator/filter_iterator.hpp:44:9: error: use of deleted function ‘TestLinqCpp()::<lambda(int)>& TestLinqCpp()::<lambda(int)>::operator=(const TestLinqCpp()::<lambda(int)>&)’
linq.cpp:24:22: note: a lambda closure type has a deleted copy assignment operator
from(v).where([](int x) { return x % 2 != 0; }).distinct().to_vector();
你好!请教一个问题,当前的设计似乎不包括下述这个情况:
当切面代码在逻辑上运行失败,需要退出,不需要执行核心代码时的处理,像这个情况,当前的实现似乎只能在切面代码中抛出异常!
我在项目中遇到的情况,在self_register_factory.hpp中,
template<typename... Args>
register_t(const std::string& key, Args... args)
{
factory::get().map_.emplace(key, [&] { return new T(args...); });
}
这里的lambda表达式应该按值("=")来捕获,如果采用引用捕获的话,之后在调用
factory::get().produce生产产品的时候可能会出现空悬引用
这两个文件编译报错,不知道为什么,请指点一下,谢谢!
MessageBus.hpp在vs2017下面编译错误如下(已经打开c++11):
Any.hpp:14: error: C2059: 语法错误:“typeid”
Any.hpp:14: error: C2059: 语法错误:“)”
Any.hpp:14: warning: C4183: “m_tpIndex”: 缺少返回类型;假定为返回“int”的成员函数
MessageBus.hpp:25: error: C3861: “R”: 找不到标识符
MessageBus.hpp:25: error: C3861: “R”: 找不到标识符
modern_messagebus.hpp在vs2017下面编译错误如下(已经打开c++11):
modern_messagebus.hpp:16: error: C2653: “timax”: 不是类或命名空间名称
modern_messagebus.hpp:16: 参见“function_traits”的声明
with
[
Function=main::<lambda_189c7c762dd8ac28cd741d0f2b8ec4ae>
]
modern_messagebus.hpp:16: 参见“function_traits”的声明
with
[
Function=main::<lambda_189c7c762dd8ac28cd741d0f2b8ec4ae>
]
请教大佬,怎么搭建开发环境啊,代码给我我都不知道怎么跑,书里面也没说怎么搭建环境。我用的clion,wingw,但是跑不起来
请问能不能给一个使用MessageFactory1.hpp这个文件的main函数,现有的main.cpp似乎无法使用
你好,
我发现书本上Empty()
函数中,出现了一个lock_guard<std::mutex>
锁,我尝试着跑代码时,发现死锁了。
void Take(std::list<T>& list) {
std::unique_lock<std::mutex> locker(mutex_);
//not_empty_.wait(locker, [this]{ return need_stop_ || !Empty(); });
while (need_stop_ || !Empty()){//lock_guard ???
not_empty_.wait(mutex_);
}
.....
not_full_.notify_one();
}
注释掉这一行,正常运行。
bool Empty() {
//std::lock_guard<std::mutex> locker(mutex_);
return queue_.empty();
}
以下是我的几点理解,不知道是否正确:
其一,wait
会在条件不满足时自动unlock
,进入waiting...
。当被其他线程notify_one()
时自动请求lock
,继续运行....
其二,虽然unique_lock
内部维护着一个变量,析构时可以自动识别是否需要unlock
,lock_guard
就比较粗暴了。
Mystruct中m_obj = lazy([]{return std::make_shared<Big>(); });
在gcc与msvc下均无法通过编译
gcc报错信息
cannot bind non-const lvalue reference of type 'Mystruct::Mystruct()::<lambda()>&' to an rvalue of type 'Mystruct::Mystruct()::<lambda()>'
将其改为
auto temp=[]{return std::make_shared<Big>(); };
m_obj = lazy(temp);
后可以通过编译
你好! RunInThread中一次把同步队列中的任务都右值引用了,这样也是单个线程里依次在跑所有的任务啊。而不是分别在各个线程中跑一个任务?
请问我的理解有错误么?谢谢
void RunInThread()
{
while (m_running)
{
//取任务分别执行
std::list<Task> list;
m_queue.Take(list);
for (auto& task : list)
{
if (!m_running)
return;
task();
}
}
}
最下面那个 std::enable_if(!std::is_same<T, string>::value ......
这里不该有这个!
如果是string类型就直接返回
你好,请问为什么SyncQueue.hpp中函数Count(),不用加上锁
从http://blog.csdn.net/afei198409/article/details/50448296 看到问题
创造函数保存在map中,
即 static std::map<std::string, std::function<Message * ()>> map_;
如果创造具体对象的条件有多个呢?该怎么去设计这个映射?嵌套map吗?
譬如,用简单工厂大致描述需求如下:
switch(factor_1) {
case 1:
switch(factor_2) {
case 1:
returan new product_1_1();
break;
case 2:
returan new product_1_2();
break;
}
break;
case 2:
switch(factor_2) {
case 1:
returan new product_2_1();
break;
case 2:
returan new product_2_2();
break;
}
break;
}
void PrintT(int& t)
{
std::cout << "lvalue" << std::endl;
}
大佬想表达的是
template <typename T>
void PrintT(T& t)
{
std::cout << "lvalue" << std::endl;
}
吧。
另外勘误里没有书中
page 78
void forwardValue(T& val)
应该是
void forwardValue(T&& val)
代码清单2-4, 两个PrintT都有问题
用法是这样的:
struct person {
std::string foo(const int& a) {
return std::to_string(a);
}
void foo1(const double& a) {
std::cout << a << std::endl;
}
};
int main()
{
auto& bus = FunctionMsgBus::get();
person p;
bus.register_handler(&person::foo, &p, "1");
bus.call(FnKey{ "1" }, 10);
std::getchar();
return 0;
}
会引起崩溃
然后查了下问题,发现如果函数参数是引用的话,会编译不过,如果函数参数是指针的话,同样会引起崩溃
你好 我在编译第九章的线程池项目时将书中的代码拆分成了头文件与cpp编译时报出几个undefined reference的错误。
项目层级目录如下:
|---main.cc
|---threadpool.h
|---threadpool.cc
|---syncqueue.h
|---syncqueue.cc
|---CMakeLists.txt
ObjectPool的Init中:
auto constructName = typeid(Constructor<Args...>).name(); //不区分引用
for (size_t i = 0; i <num; i++){
m_object_map.emplace(constructName, shared_ptr<T>(new T(std::forward<Args>(args)...), [this, constructName](T* p) //删除器中不直接删除对象,而是回收到对象池中,以供下次使用
{
m_object_map.emplace(std::move(constructName), std::shared_ptr<T>(p));
}));
}
当对象第一次被回收之后重新加入map时没有再次添加删除器,那么第二次删除之后就没有了。但第三次再次获取时就会获得空指针,如:
ObjectPool<Test> objectPool;
objectPool.Init(2, 3, 4.5);
{
{
auto t1 = objectPool.Get<int, double>(); t1->print();
auto t2 = objectPool.Get<int, double>(); t2->print();
}
auto t1 = objectPool.Get<int, double>(); t1->print();
auto t2 = objectPool.Get<int, double>(); t2->print();
}
auto t1 = objectPool.Get<int, double>(); t1->print();
auto t2 = objectPool.Get<int, double>(); t2->print();
运行《深入应用C++11代码优化与工程级应用》。中139页的测试列子通过不了。您是否没有上传最新的代码
问一下版主 Aspect.hpp
怎么织入类函数成员,带参数变量(切面函数before,after同样传入参数?)
请教下,编译的时候报错:
message_bus.h:56:32: error: 'timax' has not been declared
function_traits.h:32:21: error: 'remove_cv_t' is not a member of 'std'
GCC 版本:5.5.0
c++11
比如,给Variant传入const类型不能自动转换成非const类型,传入const char*不能自动转换成string, 例子如下
const int i = 0;
Variant<std::string, int> v1{i}; //报错
Variant<std::string, int> v2{"hello"}; //报错
Variant<std::string, int> v3{int(i)}; //正确
Variant<std::string, int> v4{std::string("world")}; //正确
template <typename T, typename... List>
struct IndexOf;
template <typename T, typename Head, typename... Rest>
struct IndexOf<T, Head, Rest...>
{
enum { value = IndexOf<T, Rest...>::value + 1 };
};
template <typename T, typename... Rest>
struct IndexOf<T, T, Rest...>
{
enum { value = 0 };
};
template
struct IndexOf
{
enum{value = -1};
};
template<typename T, typename...Args>
constexpr auto IndexOf_v = IndexOf<T, Args...>::value;
//error:如果不含T类型,返回结果是错误的
IndexOf_v<int, char, char*, double, int64> = 3
template<typename T, typename... Args>
constexpr auto IndexOf_v = std::conditional_t<Contains_v<T, Args...>,
IndexOf<T, Args...>, std::integral_constant<int32_t, -1>>::value;
我借助Contains模板首先查找是否有匹配的类型,然后再进行IndexOf模板操作来避免这个问题。不知道怎么来修改这个IndexOf模板来直接解决这个不含T类型时的错误问题。你能提供一些帮助吗?
输出前一天和后一天的例子中,next 应该是 + 号
xxxxx::to_time_t(now + hours(24))
aop切面的递归展开只有最里层才允许没有Before或者After其中一个,外层全部都必须要Before和After
和博客里的设计初衷有偏差 https://www.cnblogs.com/qicosmos/p/4772389.html
Hoping put it in order.
你好,最近在看你的书,书很好。
在第9章发现一个潜在的bug。
SyncQueue.hpp
void Take(T& t)
{
......
if (m_needStop)
return;
}
如果这里不给引用t赋值的话,在ThreadPool
中调用Take(T& t)
,后直接调用t();
就会产生异常
这样是不是更好一些?
void Take(T& t)
{
......
if (m_needStop)
{
t = [] { };
return;
}
}
在LinqCpp.hpp的下面代码中:
template
auto last(const F& f) -> decltype(reverse().first(f))
{
return reverse().first(f);
}
遇到了如下编译错误:
LinqCpp.hpp(72,44): error C2672: 'reverse': no matching overloaded function found
环境:
visual studio 2019
c++17
boost_1_76_0-msvc-14.2-32
他是一个特殊的,匿名的非nunion --》 应该是匿名的non-union
IoC 章节中,RegisterSimple 在这个代码仓库里没找到
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.