Giter Club home page Giter Club logo

cosmos's Introduction

cosmos

桃李不言,下自成蹊。

尽自己一份力,让c++的世界变得更美好!

C++开源社区:http://purecpp.org/

cosmos's People

Contributors

liuping001 avatar lucklove avatar qicosmos avatar zerger avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cosmos's Issues

ThreadPool.hpp中RunInThread函数的一点疑问。

你好! RunInThread中一次把同步队列中的任务都右值引用了,这样也是单个线程里依次在跑所有的任务啊。而不是分别在各个线程中跑一个任务?
请问我的理解有错误么?谢谢

    void RunInThread()
    {
        while (m_running)
        {
            //取任务分别执行
            std::list<Task> list;
            m_queue.Take(list);

            for (auto& task : list)
            {
                if (!m_running)
                    return;

                task();
            }
        }
    }

Lazy测试代码无法通过编译

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);
后可以通过编译

IndexOf result error if not match T

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类型时的错误问题。你能提供一些帮助吗?

关于对象池模式SimpleObjectPool.hpp中的问题

代码如下
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这句的作用是什么呢?一直没看懂

如何知道MessageBus中传入函数的所有参数类型

我想问如何知道Attach函数的输入函数F的所有形参类型?
Attach函数原型如下:
//注册消息
template
void Attach(F&& f, const string& strTopic="")
{
auto func = to_function(std::forward(f));
Add(strTopic, std::move(func));
}

因为我需要记录这些形参类型,在调用的时候首先尝试进行强制转换。
感谢回复

cpp11 book 2-1

abi::__cxa_demangle(typeid(TR).name()), nullptr,
nullptr, nullptr),

这里括号没对,应该是

abi::__cxa_demangle(typeid(TR).name() , nullptr,
nullptr, nullptr),

cpp11 book page 59

他是一个特殊的,匿名的非nunion --》 应该是匿名的non-union

自动注册工厂出现空悬引用

我在项目中遇到的情况,在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生产产品的时候可能会出现空悬引用

CPP11 BOOK Page 102

最下面那个 std::enable_if(!std::is_same<T, string>::value ......

这里不该有这个!

如果是string类型就直接返回

function message bus,注册的函数无法支持引用和指针类型

用法是这样的:
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;

}
会引起崩溃
然后查了下问题,发现如果函数参数是引用的话,会编译不过,如果函数参数是指针的话,同样会引起崩溃

MessageBus modern_messagebus编译报错!

这两个文件编译报错,不知道为什么,请指点一下,谢谢!
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>
]

modern_messagebus.hpp是不是有啥问题?

请教下,编译的时候报错:
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

LinqCpp编译报错,尝试了各种办法,没能解决,求助。

在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

cpp11 book 2-4

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都有问题

关于轻量级AOP框架实现的一个疑问

你好!请教一个问题,当前的设计似乎不包括下述这个情况:
当切面代码在逻辑上运行失败,需要退出,不需要执行核心代码时的处理,像这个情况,当前的实现似乎只能在切面代码中抛出异常!

编译LinqCpp出错

你好!我在编译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();

似乎死锁? not_empty_.wait(locker, [this]{ return need_stop_ || !Empty(); });

你好,

我发现书本上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内部维护着一个变量,析构时可以自动识别是否需要unlocklock_guard就比较粗暴了。

SyncQueue Take(T& t) bug

你好,最近在看你的书,书很好。
在第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;
    }
}

CPP11 BOOK Page 189

输出前一天和后一天的例子中,next 应该是 + 号

xxxxx::to_time_t(now + hours(24))

如果创造具体对象的条件有多个呢?

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;
}

ObjectPool逻辑可能有问题

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();

关于SyncQueue.hpp中的Take(std::list<T> list)方法的std::move操作

您好,看了您的书依然对于std::move这部分不是很理解
在SyncQueue.hpp 第34行 list = std::move(m_queue) 之后对于m_queue没有任何操作,我认为这样做,之后的线程再对m_queue进行的操作都会导致错误,因为这时的m_queue已经变成了 'list' 交给工作线程去处理了,所以应该更新m_queue。。我还没想好应该怎样去更新,也不知道自己的想法是否正确。。希望您能解答我的疑问。

Variant构造时不能自动调用一些转换的bug

比如,给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")};     //正确

开发环境搭建

请教大佬,怎么搭建开发环境啊,代码给我我都不知道怎么跑,书里面也没说怎么搭建环境。我用的clion,wingw,但是跑不起来

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.