小编Mic*_*ael的帖子

C++ 模板参数数量错误(2,应该是 1)

我使用 C++ 并行快速排序程序进行了测试,如下所示,首先使用列表作为容器,然后我转移到通用容器类型,但它报告了标题错误。

\n\n

可以帮忙解决这个问题吗?

\n\n
#include <iostream>     // std::cout\n#include <future>       // std::packaged_task, std::future\n#include <chrono>       // std::chrono::seconds\n#include <thread>       // std::thread, std::this_thread::sleep_for\n#include <list>\n#include <algorithm>\n#include <type_traits>\n#include <iterator>\n\ntemplate<typename F, typename A>\nstatic std::future<typename std::result_of<F(A&&)>::type> spawn_task(F&& f, A&& a)\n{\n    typedef typename std::result_of<F(A&&)>::type result_type;\n\n    std::packaged_task<result_type(A&&)> task(std::move(f));\n    std::future<result_type> res(task.get_future());\n    std::thread myThread(std::move(task), std::move(a));\n    myThread.detach();\n    return res;\n}\n\ntemplate<class T, template<class T> class Container>\n\nstatic Container<T> parallel_quick_sort(Container<T> input)\n{\n    if (input.empty())\n    {\n        return input;\n    }\n\n    Container<T> result;\n    result.splice(result.begin(), input, input.begin());\n    T const& partition_val = *result.begin();\n\n    typename Container<T>::iterator divide_point = std::partition\n    (input.begin(), input.end(), [&](T …
Run Code Online (Sandbox Code Playgroud)

c++ templates

5
推荐指数
1
解决办法
2万
查看次数

正确使用std move为工厂类

我想创建一个像下面这样的工厂类,但我不相信这是使用std :: move的正确方法.我不想使用太多的shared_ptrs,因为另一个shared_ptr中的shared_ptr真的很丑陋,有时令人困惑......

选项1:

class Foo
{
  public:
  Foo(Foo&& f){...}
}

class FooFactory
{
   public:
     static Foo&& createFoo(...)
     {
       Foo temp(...);
       return std::move(temp);
     }
}

main()
{
   Foo f=FooFactory::createFoo(...);
}
Run Code Online (Sandbox Code Playgroud)

选项2:

class FooFactory
{
   public:
     static Foo createFoo(...)
     {
       Foo temp(...);
       return temp;
     }// rely on compiler for optimization
}

main()
{
   Foo f=std::move(FooFactory::createFoo(...));
}
Run Code Online (Sandbox Code Playgroud)

c++ move-semantics c++11

4
推荐指数
1
解决办法
966
查看次数

C++模板:如何通过std :: is_pointer有条件地删除值

在我做的一般容器的析构函数中,我尝试删除元素,如果它们是指针,所以我尝试下面.但是,当我使用T = double进行测试时,编译器显示错误消息,即删除必须后跟指针.我该怎么做呢?

template<class T> static void deleteIfPointer(T t)
{

    if(std::is_pointer<T>::value)
    {
        std::cout << "is pointer" << std::endl;
        delete t;
    }
    else
        std::cout << "not pointer" << std::endl;

}
Run Code Online (Sandbox Code Playgroud)

c++ templates

2
推荐指数
1
解决办法
217
查看次数

strcpy_s for char**和char [] []

我使用strcpy_s如下:

char names[2][20];
strcpy_s(names[0],"Michael");
strcpy_s(names[1],"Danny");
Run Code Online (Sandbox Code Playgroud)

它工作得很好.

但是,当我改char **,

int size1=2;
int size2=20;

char **names=new char*[size1];
for(int i=0;i<size1;i++)
  names[i]=new char[size2];
strcpy_s(names[0],"Michael");
strcpy_s(names[1],"Danny");
Run Code Online (Sandbox Code Playgroud)

它给了我这个错误信息:

错误C2660:'strcpy_s':函数不带2个参数

为什么会这样?我需要动态创建char数组,所以我该怎么办?

c++ arrays char strcpy

1
推荐指数
1
解决办法
7888
查看次数

提升线程死锁,任何人都可以检查为什么?

我试着编写一个可以进行多线程读写的程序.它可以在一次读取和一次写入时正常工作,但在使用两次读取和一次写入时,它以死锁结束.

谁能帮忙检查一下?

const int BUF_SIZE = 10;
const int ITERS = 100;

boost::mutex io_mutex;

class buffer
{
public:

    typedef boost::mutex::scoped_lock scoped_lock;

    buffer() : p(0), c(0), full(0)
    {}

    void put(int m)
    {
        scoped_lock lock(mutex);
        if (full == BUF_SIZE)
        {
            {
                boost::mutex::scoped_lock lock(io_mutex);
                std::cout << "Buffer is full. Waiting..." << std::endl;
            }
            while (full == BUF_SIZE)
                cond.wait(lock);
        }

        buf[p] = m;

        p = (p+1) % BUF_SIZE;

        ++full;

        cond.notify_all();
    }

    int get()
    {
        scoped_lock lk(mutex);

        if (full == 0)
        {
            {
                boost::mutex::scoped_lock lock(io_mutex); …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading boost deadlock

0
推荐指数
1
解决办法
672
查看次数

带模板化参数的C++模板特化

我尝试编写一个名为Null的模板化类,如下所示;

template <class Type>
class Null;

template <>
class Null<std::string> {
  public:
    Null() {}
    operator std::string() const {
        return std::string();
    }
};
Run Code Online (Sandbox Code Playgroud)

到目前为止适用于字符串

但我想写点类似的东西

template<> class Null<boost::shared_ptr<T>>
{
public:
    NUll(){}
    operator boost::shared_ptr<T>() const
    {
        return boost::shared_ptr<T>();
    }
};
Run Code Online (Sandbox Code Playgroud)

但它不会编译,我测试了其他方法,无法解决.我怎么能这样做?

c++ templates

0
推荐指数
1
解决办法
65
查看次数