我正在设置一些预处理器的东西,我想要一个更准确的数字,因为__cplusplusC++ 14应该被定义为.是否有一个标准强制要求?
我想这样做:
using function_type = void (*)(void*)noexcept;
Run Code Online (Sandbox Code Playgroud)
但是我收到错误"类型别名中不允许出现异常规范." (Xcode 6.1版中的clang)
有没有使用noexcept说明符创建别名的解决方法?
我正在寻找一些适用于跨平台功能的语言(不是扩展)定义的东西.
我为一个非常简单的数据写了一个容器,需要跨线程同步.我想要最好的表现.我不想使用锁.
我想用"放松"的原子.部分是为了一点额外的魅力,部分是为了真正理解它们.
我一直在研究这个问题,而且我正处于这个代码通过我抛出的所有测试的地步.但这并不是"证据",所以我想知道是否有任何我遗漏的东西,或者我可以测试的其他任何方式?
这是我的前提:
这就是我在想的."通常",我们对我们正在阅读的代码进行推理的方式是查看它所编写的顺序.内存可以被读取或写入"乱序",但不能使程序的正确性无效.
这在多线程环境中发生了变化.这就是内存栅栏的用途 - 这样我们仍然可以查看代码并能够推断它是如何工作的.
所以,如果一切都可以在这里完全失序,我在放松原子的做法是什么?这不是有点太远吗?
我不这么认为,但这就是我在这里寻求帮助的原因.
compare_exchange操作本身可以保证彼此之间具有连续的恒定性.
读取或写入原子的唯一另一个时间是在compare_exchange之前获取头部的初始值.它被设置为变量初始化的一部分.据我所知,这个操作是否带回了"适当的"值是无关紧要的.
当前代码:
struct node
{
node *n_;
#if PROCESSOR_BITS == 64
inline constexpr node() : n_{ nullptr } { }
inline constexpr node(node* n) : n_{ n } { }
inline void tag(const stack_tag_t t) { reinterpret_cast<stack_tag_t*>(this)[3] = t; }
inline stack_tag_t read_tag() { return reinterpret_cast<stack_tag_t*>(this)[3]; }
inline void clear_pointer() { tag(0); }
#elif PROCESSOR_BITS == 32
stack_tag_t t_;
inline constexpr node() …Run Code Online (Sandbox Code Playgroud) 我想创建一个像这样的函数指针:
void(*function_pointer)()noexcept;
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用.似乎函数声明中的异常说明符无效.必须有办法做到这一点.对?
这与一个与此不同的问题有关.在这里,我问如何使用noexcept说明符创建一个函数指针.在"函数typedef中的noexcept说明符"这个问题中没有被问到或回答过.
我知道这可能听起来像一个奇怪的问题,但我只是想知道C++中的类是否比具有相同数据字段的结构更重要,并且有一件事我无法找到答案...
考虑一下:
struct SomeStruct {
int a;
int b;
};
class SomeClass {
public:
SomeClass():a(0),b(0){}
private:
int a;
int b;
};
int main() {
std::cout<<sizeof(SomeStruct)<<std::endl; // output is 8
std::cout<<sizeof(SomeClass)<<std::endl; // output is 8
}
Run Code Online (Sandbox Code Playgroud)
但现在看看当我向SomeClass添加析构函数时会发生什么:
struct SomeStruct {
int a;
int b;
};
class SomeClass {
public:
SomeClass():a(0),b(0){}
virtual ~SomeClass(){}
private:
int a;
int b;
};
int main() {
std::cout<<sizeof(SomeStruct)<<std::endl; // output is 8 bytes
std::cout<<sizeof(SomeClass)<<std::endl; // output is 16 bytes!
}
Run Code Online (Sandbox Code Playgroud)
为什么SomeClass需要8个字节用于析构函数?
每次在Xcode中进行编译时,每次尝试等待condition_variable时都会收到错误消息。错误是“以类型为std :: __ 1 :: system_error的未捕获异常终止:条件变量等待失败:参数无效”
一切在Visual Studio 2013中都可以正常工作。如果我决定不等待多个线程的相同condition_variable,则代码可以正常工作。r
好的,代码。
main.cpp:
#include "ThreadPool.h"
int main(int argc, const char * argv[])
{
ThreadPool pool;
for (int i = 0; i < 10; ++i)
pool.sendWork();
std::this_thread::sleep_for(std::chrono::milliseconds(50000));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
线程池
#pragma once
#include <condition_variable>
#include <vector>
#include <thread>
class ThreadPool
{
protected:
std::condition_variable _condition;
private:
std::vector<std::thread> _threads;
void threadLoop();
public:
ThreadPool();
void sendWork();
};
Run Code Online (Sandbox Code Playgroud)
ThreadPool.cpp:
#include "ThreadPool.h"
ThreadPool::ThreadPool()
{
for (unsigned int i {0}; i < 10; ++i)
_threads.push_back(std::thread(&ThreadPool::threadLoop, this));
}
void …Run Code Online (Sandbox Code Playgroud) 大脑疼了吗?
我正在研究tuple类型转换助手,并且遇到了问题.要么我重复代码,要么我包装template template.虽然我无法解决这个问题.
这是我需要使用的:
template<template<typename> class trans> struct no_index_transformer
{
template<size_t, typename transform_t> struct transformer
{
using t = typename trans<transform_t>::t;
};
};
Run Code Online (Sandbox Code Playgroud)
no_index_transformer采取template template的transformer是不具有size_t(指数)传递给它.内部transformer template是我需要通过的.我这样做的时候会出错,所以我不确定通过什么是正确的方法no_index_transformer<>::transformer.
我在以下行中收到错误:
template<template<typename> class transformer, typename tuple> using transform_tuple_t
= transform_tuple_index<no_index_transformer<transformer>::transformer, 0, tuple>::t;
Run Code Online (Sandbox Code Playgroud)
"模板模板参数的模板参数必须是类模板或类型别名模板."
这是变压器:
template<typename, typename> struct tuple_cat;
template<typename... types_one, typename... types_two> struct tuple_cat<std::tuple<types_one...>, std::tuple<types_two...>>
{
public:
using t = std::tuple<types_one..., types_two...>;
};
template<template<size_t, typename> class transformer, size_t index, …Run Code Online (Sandbox Code Playgroud) 在C++中,有些东西出现在明确定义和未定义之间.具体而言,这些被称为实现定义和未指定.现在,我对未指明的东西很感兴趣.
什么时候可以使用这些功能,什么时候应该避免?是否有正确代码的未指定行为的良好示例?在编写软件时,它是否是最佳选择?
Matt McNabb提供的定义:
未定义 - 任何事情都可能发生
实现定义 - 有限数量的结果是可能的,编译器的文档必须说明会发生什么
未指定 - 可能有限数量的结果 - 通常标准描述了可能结果的集合
定义明确 - 以上都不是
格式良好的程序 - 无错误编译的程序(可能表现出未定义的行为)
后续问题:
放松的原子是否被指定为未指定或明确定义?