C++ c++;没有重载函数的实例,但使用模板typename。。。?

C++ c++;没有重载函数的实例,但使用模板typename。。。?,c++,c++11,C++,C++11,我试图从一本书中收集一些示例代码。。但是这本书和github副本是不同的。。我想我很快就会有一个线程池的工作示例,它接受函数并包装它们,这样您就可以等待它们的值作为未来返回。。。但是在模板化过程中出现编译错误 我试着在helloworld.cpp的末尾实例化我需要的确切类,就像在 但是接下来会得到一系列关于试图暗示一个我已经设置为=delete的函数的警告 明白我的意思吗 我有点新的C++,仍然不确定如何最好地继续,编译错误在最后。我已经测试了thread\u safe\u queue.cpp

我试图从一本书中收集一些示例代码。。但是这本书和github副本是不同的。。我想我很快就会有一个线程池的工作示例,它接受函数并包装它们,这样您就可以等待它们的值作为未来返回。。。但是在模板化过程中出现编译错误

我试着在
helloworld.cpp
的末尾实例化我需要的确切类,就像在

但是接下来会得到一系列关于试图暗示一个我已经设置为
=delete
的函数的警告

明白我的意思吗

<>我有点新的C++,仍然不确定如何最好地继续,编译错误在最后。我已经测试了
thread\u safe\u queue.cpp
,并且它已经在其他更简单的用途中发挥了作用,所以我不认为这是错误的。。。更重要的是,模板需要帮助

helloworld.cpp

#include <thread>
#include <vector>
#include <atomic>
#include <iostream>
#include <future>
#include <functional>

#include <unistd.h>

 // https://github.com/anthonywilliams/ccia_code_samples/blob/6e7ae1d66dbd2e8f1ad18a5cf5c6d25a37b92388/listings/listing_9.1.cpp
 // https://github.com/anthonywilliams/ccia_code_samples/blob/6e7ae1d66dbd2e8f1ad18a5cf5c6d25a37b92388/listings/listing_9.2.cpp

#include "thread_safe_queue.cpp"

class function_wrapper
{
    struct impl_base {
        virtual void call()=0;
        virtual ~impl_base() {}
    };
    std::unique_ptr<impl_base> impl;
    template<typename F>
    struct impl_type: impl_base
    {
        F f;
        impl_type(F&& f_): f(std::move(f_)) {}
        void call() { f(); }
    };
public:
    template<typename F>
    function_wrapper(F&& f):
        impl(new impl_type<F>(std::move(f)))
    {}

    void call() { impl->call(); }

    function_wrapper(function_wrapper&& other):
        impl(std::move(other.impl))
    {}

    function_wrapper& operator=(function_wrapper&& other)
    {
        impl=std::move(other.impl);
        return *this;
    }

    function_wrapper(const function_wrapper&)=delete;
    function_wrapper(function_wrapper&)=delete;
    function_wrapper& operator=(const function_wrapper&)=delete;
};

struct join_threads
{
    join_threads(std::vector<std::thread>&)
    {}
};

class thread_pool
{
    std::atomic_bool done;
    thread_safe_queue<function_wrapper> work_queue;
    std::vector<std::thread> threads;
    join_threads joiner;

    void worker_thread()
    {
        while(!done)
        {
            std::function<void()> task;
            if(work_queue.try_pop(task))
            {
                task();
            }
            else
            {
                std::this_thread::yield();
            }
        }
    }
public:
    thread_pool():
        done(false),joiner(threads)
    {
        unsigned const thread_count=std::thread::hardware_concurrency();
        try
        {
            for(unsigned i=0;i<thread_count;++i)
            {
                threads.push_back(
                    std::thread(&thread_pool::worker_thread,this));
            }
        }
        catch(...)
        {
            done=true;
            throw;
        }
    }

    ~thread_pool()
    {
        done=true;
    }

    template<typename FunctionType>
    std::future<typename std::result_of<FunctionType()>::type>
    submit(FunctionType f)
    {
        typedef typename std::result_of<FunctionType()>::type result_type;
        
        std::packaged_task<result_type()> task(std::move(f));
        std::future<result_type> res(task.get_future());
        work_queue.push_back(std::move(task));
        return res;
    }
};

void find_the_answer_to_ltuae(){
    std::cout << "About to sleep 1 second...";
    sleep(1);
    std::cout<<"The answer is " << 42 << std::endl;
}

int main()
{
    std::cout << "About to create my_threadpool...." << std::endl;
    thread_pool my_threadpool;
    std::cout << "Done creating my_threadpool...." << std::endl;
    std::cout << "Submitting first job now" << std::endl;
    my_threadpool.submit(find_the_answer_to_ltuae);
    sleep(10);
    std::cout <<"Finished" << std::endl;
}

template class thread_safe_queue<function_wrapper>; // <-------- this was added by me, in an attempt to get the templating happy.. didn't work
#include <mutex>
#include <memory>
#include <condition_variable>
#include <queue>

template <typename T>
class thread_safe_queue {
    public:
        // constructor
        thread_safe_queue() {}
        thread_safe_queue(const thread_safe_queue& other)
        {
            std::lock_guard<std::mutex> lock{other.mutex};
            queue = other.queue;
        }

        void push(T new_value)
        {
            std::lock_guard<std::mutex> lock{mutex};
            queue.push(new_value);
            cond.notify_one();
        }

        void wait_and_pop(T& value)
        {
            std::unique_lock<std::mutex> lock{mutex};
            cond.wait(lock, [this]{ return !queue.empty(); });
            value = queue.front();
            queue.pop();
        }

        std::shared_ptr<T> wait_and_pop()
        {
            std::unique_lock<std::mutex> lock{mutex};
            cond.wait(lock, [this]{ return !queue.empty(); });
            std::shared_ptr<T> res{std::make_shared<T>(queue.front())};
            queue.pop();
            return res;
        }

        bool try_pop(T& value)
        {
            std::lock_guard<std::mutex> lock{mutex};
            if (queue.empty())
                return false;
            value = queue.front();
            queue.pop();
            return true;
        }

        std::shared_ptr<T> try_pop()
        {
            std::lock_guard<std::mutex> lock{mutex};
            if (queue.empty())
                return std::shared_ptr<T>{};
            std::shared_ptr<std::mutex> res{std::make_shared<T>(queue.front())};
            queue.pop();
            return res;
        }

        bool empty() const
        {
            std::lock_guard<std::mutex> lock{mutex};
            return queue.empty();
        }

    private:
        mutable std::mutex mutex;
        std::condition_variable cond;
        std::queue<T> queue;
};


//https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file
#包括
#包括
#包括
#包括
#包括
#包括
#包括
// https://github.com/anthonywilliams/ccia_code_samples/blob/6e7ae1d66dbd2e8f1ad18a5cf5c6d25a37b92388/listings/listing_9.1.cpp
// https://github.com/anthonywilliams/ccia_code_samples/blob/6e7ae1d66dbd2e8f1ad18a5cf5c6d25a37b92388/listings/listing_9.2.cpp
#包括“线程安全队列.cpp”
类函数包装器
{
结构impl_base{
虚空调用()=0;
虚~impl_base(){}
};
std::唯一的\u ptr impl;
模板
结构impl_类型:impl_base
{
F;
impl_类型(F&&F_):F(std::move(F_)){
void调用(){f();}
};
公众:
模板
函数包装器(F&&F):
impl(新的impl_类型(std::move(f)))
{}
void call(){impl->call();}
函数包装器(函数包装器和其他):
impl(std::move(other.impl))
{}
函数包装器和运算符=(函数包装器和其他)
{
impl=std::move(other.impl);
归还*这个;
}
函数包装器(常量函数包装器&)=删除;
函数包装器(函数包装器&)=删除;
函数_wrapper&运算符=(常量函数_wrapper&)=delete;
};
结构连接线程
{
连接线程(std::vector&)
{}
};
类线程池
{
标准::原子波完成;
线程安全队列工作队列;
向量线程;
连接螺纹连接件;
无效工作线程()
{
而(!完成)
{
功能任务;
如果(工作队列。尝试弹出(任务))
{
任务();
}
其他的
{
std::this_thread::yield();
}
}
}
公众:
线程池():
完成(错误),接合(螺纹)
{
unsigned const thread_count=std::thread::hardware_concurrency();
尝试
{
对于(无符号i=0;对于

/src/helloworld.cpp:70:27: error: no matching member function for call to 'try_pop'
        if(work_queue.try_pop(task))
    
您的问题是,
task
是一个
std::function
,但是
try\u-pop
需要一个
function\u-wrapper&
std::function
没有
operator-function\u-wrapper&
,因此您不能将其传递给
try\u-pop
。您需要做的是更改
try\u-pop
以获取de>const T&
,这样就可以创建一个临时的
函数包装器
,或者创建您自己的
函数包装器
,包装
任务
,并将其传递给
try\u pop

你的第二个错误

/src/helloworld.cpp:113:20: error: no member named 'push_back' in 'thread_safe_queue<function_wrapper>'
    work_queue.push_back(std::move(task));
    


感谢@nathanoliver,我最终将任务类型更改为function_wrapper..我认为该书作者故意弄乱了git中的代码,因此你必须购买该书..出现其他错误,但会继续玩。谢谢!
/src/helloworld.cpp:113:20: error: no member named 'push_back' in 'thread_safe_queue<function_wrapper>'
    work_queue.push_back(std::move(task));
    
work_queue.push_back(std::move(task));
work_queue.push(std::move(task));