小编One*_*Pie的帖子

std :: rethrow_exception因嵌套线程而失败

我有一个程序需要将异常从任何未被捕获的线程传播到主线程,以便它可以向用户提供有关失败原因和安全关闭的信息.我的InterThreadException处理程序似乎工作,但只有当抛出异常的线程由主线程直接生成时.

该InterTheadException处理器在所有情况下都正确地调用,而被发信具有传递给它的异常指针的异常传播,并在主线程接收到它已经收到了新的异常通知,但调用std::rethrow_exception的异常指针只是失败似乎什么也没做.我已经测试过从这两个不同的线程中抛出完全相同的异常,并且无论我抛出什么,问题似乎都会持续存在.

我怀疑我对如何使用异常指针有一个基本的误解,但我不确定.

这是我的实施InterThreadExceptionHandler.

class InterThreadExceptionHandler : NonCopyable
{
public:
    InterThreadExceptionHandler();
    ~InterThreadExceptionHandler();

    //
    // Sends an exception into this handler, informing it that 
    //this exception has been caught
    // and may be propagated to another thread
    //
    void sendException(std::exception_ptr exception);

    //
    // Freezes the calling thread until an exception has been sent,
    //then rethrows said exception
    //
    void waitForException();

private:
    std::vector<std::exception_ptr> sentExceptions; 
    std::mutex sentExceptionsMutex;

    Semaphore sentExceptionsCounter;
};


InterThreadExceptionHandler::InterThreadExceptionHandler()
{
}
InterThreadExceptionHandler::~InterThreadExceptionHandler()
{ …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading exception c++11 visual-studio-2012

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

使用c ++ 11构造映射函数

两者都教我自己实现更高级的模板结构而不仅仅是基本的模板结构,并且因为它们在许多情况下都很有用,我正在尝试使用像decltype这样的c ++ 11结构来实现函数式编程中常见的map,filter和类似函数.

我在创建我使用的编译器可以处理的函数原型时遇到了麻烦,所以我不得不问你如何创建这样的东西:

//
// Takes an iterable, applies a function to every element, and returns a vector of the results
//
template <typename T, typename Func>
auto map(const T& iterable, Func func) -> std::vector< decltype(  func( *iterable.cbegin() ) ) >
{
    // body snipped
}
Run Code Online (Sandbox Code Playgroud)

也就是说,此函数应该采用任何iterable和一个函数,该函数将iterables值类型作为参数并返回某种值.函数调用的结果将是传递函数返回的类型的向量,无论传入的可迭代类型如何.

map函数应接受任何具有有效原型作为参数的函数,无论它是函数指针,函子还是lambda表达式.

使用上面的函数与此测试代码:

std::vector<int> intVector;
intVector.push_back(1);
intVector.push_back(2);

map(intVector, [](int& value) { return value + 1; });
Run Code Online (Sandbox Code Playgroud)

使visual studio吐出C2893("专业化功能模板失败")错误,我不确定是什么问题.

更新: 到目前为止,在评论和答案中建议应用的更改,新的原型测试但仍然存在相同的错误.

c++ templates metaprogramming c++11 visual-studio-2012

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

使用 c++ 20 Concept 检查函数是否返回 const 值

我正在尝试使用概念来检测给定类型的开始函数是否将迭代器返回到“const T&”或仅返回到“T&”。但我不确定如何最好地去做,我尝试了以下方法:

#include <iostream>
#include <concepts>
#include <vector>
#include <set>

template <typename T>
concept IsConst = std::is_const<T>::value;

template<typename T>
concept IsIterableOfConst = requires (T& t) { { *t.begin() } -> IsConst; };

template <IsIterableOfConst T>
void test()
{
    std::cout << "Yes" << std::endl;
}

template <typename T>
void test()
{
    std::cout << "No" << std::endl;
}


int main(int argc, char** argv)
{
    test<std::set<int>>();
    test<std::vector<int>>();
}
Run Code Online (Sandbox Code Playgroud)

这会产生输出“No No”,当我希望它产生输出“Yes No”时,因为 std::set 中的迭代器应该,据我所知,总是迭代持有类型的集合的“const”版本. 如何正确检测容器包含 const 值?

c++ c++-concepts c++20

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