今天我偶然发现了一个像这样的代码片段:
class A
{
A() = default;
A (const A&) = delete;
...
}
Run Code Online (Sandbox Code Playgroud)
我从未见过delete或default关键字.它们是C++ 11标准的一部分吗?它们用于什么?
我正在玩索引技巧,看看我可以去哪里,并遇到一个奇怪的错误...首先,简单的不那么老的指数:
template<std::size_t...>
struct indices {};
template<std::size_t N, std::size_t... Indices>
struct make_indices:
make_indices<N-1, N-1, Indices...>
{};
template<std::size_t... Indices>
struct make_indices<0, Indices...>:
indices<Indices...>
{};
Run Code Online (Sandbox Code Playgroud)
我创建了一个从a派生的编译时数组类std::initializer_list,并使其具有可索引性(假设N3471支持您的编译器.无论如何它将在下一个标准中).这里是:
template<typename T>
struct array:
public std::initializer_list<T>
{
constexpr array(std::initializer_list<T> values):
std::initializer_list<T>(values)
{}
constexpr auto operator[](std::size_t n)
-> T
{
return this->begin()[n];
}
};
Run Code Online (Sandbox Code Playgroud)
所以,我尝试创建一个函数,array在每个成员添加1后返回一个副本:
template<typename T, std::size_t... I>
auto constexpr add_one(const array<T>& a, indices<I...>)
-> const array<T>
{
return { (a[I]+1)... };
}
Run Code Online (Sandbox Code Playgroud)
并完成代码,这是我的主要:
int main()
{
constexpr array<int> a = …Run Code Online (Sandbox Code Playgroud) 有人给我一个例子来使用以下构造函数int Qt吗?
QVector::QVector(std::initializer_list<T> args);
Run Code Online (Sandbox Code Playgroud) 我有一个简单的布尔值,我需要以线程安全的方式进行测试和设置.如果一个线程已经在工作,我希望第二个线程退出.如果我理解std::atomic_flag正确,这应该工作正常.但是,我不自信我理解std::atomic_flag正确:)我似乎无法在网上找到很多简单的例子,除了这个螺旋锁示例:
// myclass.cpp
#using <atomic>
namespace // anonymous namespace
{
std::atomic_flag _my_flag = ATOMIC_FLAG_INIT;
} // ns
myclass::do_something()
{
if ( !::_my_flag.test_and_set() ) )
{
// do my stuff here; handle errors and clear flag when done
try
{
// do my stuff here
}
catch ( ... )
{
// handle exception
}
::_my_flag.clear(); // clear my flag, we're done doing stuff
}
// else, we're already doing something in another thread, let's exit
} …Run Code Online (Sandbox Code Playgroud) 是否有任何网站比较实施/编译器之间当前的C11标准一致性/支持?(gcc,clang,intel,open64,pelles)
不同的Rebol 3分支之间有什么区别,特别是新的REN分支?
它们是运行的平台,功能集,代码组织,C标准合规性吗?
我最近想出了一个问题.我实际上认为它不能像我希望的那样得到解决,但如果可能的话它会非常方便.无论如何,这是问题所在:
我将在几天前在这个论坛上看到一个例子,因为它更容易用它来解释.假设我正在尝试以这种方式创建Tensor结构:
template <int N>
struct Tensor
{
Tensor<N - 1> x;
Tensor<N - 1> y;
Tensor<N - 1> z;
};
Run Code Online (Sandbox Code Playgroud)
为了避免无限递归,我必须为N = 1编写模板特化.
template<>
struct Tensor<1>
{
double x;
double y;
double z;
};
Run Code Online (Sandbox Code Playgroud)
实际上,当N = 1时,这个Tensor实际上是一个Vector(物理的).假设我已经以这种方式定义了Vector结构:
struct Vector
{
double x;
double y;
double z;
};
Run Code Online (Sandbox Code Playgroud)
这个结构与Tensor <1>完全相同.由于Vector结构已经存在,并且假设我自己没有实现它,我希望能够使Tensor <1>结构成为Vector结构的别名.就像typedef一样.所以,我想这样做:
// C++03
typedef Vector Tensor<1>;
// or C++11
using Tensor<1> = Vector;
Run Code Online (Sandbox Code Playgroud)
这样,Tensor <1>和Vector将是完全相同的结构,所以我可以在程序中的任何地方使用一个而不是另一个,我不需要两次编写相同的结构.
但是,实际上不可能以这种方式定义模板专业化.如果是的话,我不会在那里问这个问题.
注意:我知道前面的例子不是很好,因为我们仍然可以这样做:
using Vector = Tensor<1>;
Run Code Online (Sandbox Code Playgroud)
但如果我想用两种不同结构的特化来做这件事,那就太麻烦了.例如,在编写可以在N维空间中计算几何的几何库时:
using Circle<2> = Hypersphere<2>;
Run Code Online (Sandbox Code Playgroud)
总而言之:有没有办法通过将模板特化定义为另一个的别名来创建模板特化?
建议包含在C++ 14(又名C++ 1y)中的是一些新的线程同步原语:锁存器和屏障.提案是
这听起来是个好主意,样本使它看起来非常适合程序员.不幸的是,我认为示例代码调用了未定义的行为.提案说latch::~latch():
摧毁闩锁.如果在其他线程处于
wait()或正在调用时销毁锁存器count_down(),则行为未定义.
请注意,它表示"in wait()"而不是"block in wait()",作为使用说明count_down().
然后提供以下示例:
第二用例的示例如下所示.我们需要加载数据然后使用许多线程处理它.加载数据是I/O绑定的,而启动线程和创建数据结构是CPU绑定的.通过并行运行这些,可以提高吞吐量.
Run Code Online (Sandbox Code Playgroud)void DoWork() { latch start_latch(1); vector<thread*> workers; for (int i = 0; i < NTHREADS; ++i) { workers.push_back(new thread([&] { // Initialize data structures. This is CPU bound. ... start_latch.wait(); // perform work ... })); } // Load input data. This is I/O bound. ... // Threads can now start processing start_latch.count_down(); }
线程唤醒和返回之间是否存在竞争条件wait(),并且当闩锁离开范围时是否会破坏闩锁?除此之外,所有thread …
我在使用常量初始化类时遇到了麻烦:
为什么使用指向同一类中成员的指针初始化会导致错误?出现错误而不使用"使用"类!
class A
{
private:
int a;
const int* const aptr;
public:
constexpr A( int _a):
a(_a)
, aptr( &a) // why aptr could not be initialized?
{}
};
class Data { } d1;
class B
{
private:
Data* dptr1;
public:
constexpr B(Data* _p): dptr1( _p) {}
};
class Use
{
static constexpr A a{2}; // fail! error: field initializer is not constant
static constexpr B b{&d1}; // works
};
Run Code Online (Sandbox Code Playgroud) 是否有任何有效和惯用的方式来执行以下操作?
std::vector<int> a = { 1, 2, 3, 4 };
std::vector<int> b = { 5, 6, 7, 8 };
for (std::size_t i = 0 ; i < a.size() ; ++i)
{
a[i] += b[i];
}
Run Code Online (Sandbox Code Playgroud)
我试图避免括号/索引表示法,只使用迭代器,以便操作与任何具有前向迭代器的容器一起使用.我想到了以下解决方案:
std::vector<int> a = { 1, 2, 3, 4 };
std::vector<int> b = { 5, 6, 7, 8 };
std::transform(a.begin(), a.end(),
b.begin(),
a.begin(),
std::plus<int>());
Run Code Online (Sandbox Code Playgroud)
然而,有冗余,a.begin()我只能使用+而不是+=.标准库中是否有一些算法允许我使用迭代器而没有任何冗余,或者我是否必须手动编写完整的循环?