当应用多用户时,除了变量类型的参数之外,还有任何廉价的方法来传递非变量类型的参数吗?
我所说的"昂贵的方式"是指:
#include <boost/variant.hpp>
#include <iostream>
#include <cstdlib>
struct A {};
struct B {};
enum class C { X, Y };
std::ostream &
operator << (std::ostream & out, C const c)
{
switch (c) {
case C::X : {
return out << "C::X";
}
case C::Y : {
return out << "C::Y";
}
default : {
break;
}
}
throw std::runtime_error("unknown C value");
}
using V = boost::variant< A, B >;
struct S
: boost::static_visitor<>
{
void
operator () (C …Run Code Online (Sandbox Code Playgroud) 是不是bug,即使我指定了标志,该std::rotate函数void在GCC 4.9中也有返回值类型-std=gnu++1y?正如它在此指出的那样,应该有意义(对于某些应用程序)返回值.
为什么std::valarray不支持自定义分配器?如何设计其内存管理?是否使用new基于或malloc基于分配器?所有其他容器通常提供指定自定义分配器的可能性.再说了,std::vector在libstdc++有相应的模板参数Allocator,我可以指定__gnu_cxx::__mt_alloc为分配器.
是否允许使用std::unique通过函数创建的迭代器std::make_move_iterator?我尝试了以下,并取得了成功:
#include <iostream>
#include <ostream>
#include <vector>
#include <algorithm>
#include <limits>
#include <iterator>
#include <cstdlib>
struct A
{
A() : i(std::numeric_limits< double >::quiet_NaN()) { std::cout << __PRETTY_FUNCTION__ << "\n"; }
A(double ii) : i(ii) { std::cout << __PRETTY_FUNCTION__ << "\n"; }
A(A const & a) : i(a.i) { std::cout << __PRETTY_FUNCTION__ << "\n"; }
A(A && a) : i(std::move(a.i)) { std::cout << __PRETTY_FUNCTION__ << "\n"; a.i = std::numeric_limits< double >::quiet_NaN(); }
A & operator …Run Code Online (Sandbox Code Playgroud) 根据这个答案,应该编译以下代码而不会出错:
#include <type_traits>
namespace
{
struct A { int i; };
volatile A a{};
static_assert(std::is_volatile< decltype(a) >{});
static_assert(std::is_volatile< decltype(a.i) >{});
}
Run Code Online (Sandbox Code Playgroud)
但是有一个很难的错误:
main.cpp:10:1: error: static_assert failed
static_assert(std::is_volatile< decltype(a.i) >{});
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
Run Code Online (Sandbox Code Playgroud)
这是一个铿锵的错误还是我错过了一些实质性的东西?
额外:
#include <type_traits>
namespace
{
struct A { int i; };
const A a{};
static_assert(std::is_const< decltype(a) >{});
static_assert(std::is_const< decltype(a.i) >{});
}
Run Code Online (Sandbox Code Playgroud)
后一个代码片段的行为完全相同.
额外:
static_assert(std::is_volatile< std::remove_pointer_t< decltype(&a.i) > >{});
Run Code Online (Sandbox Code Playgroud)
不会导致错误.
鉴于两个容器:std::list< int > a;和std::list< int > b;- a.size() == b.size().需要对容器a进行b同步排序,即每个元素a交换应该导致交换相应的元素b(位置索引意义上的对应关系).假设,元素中a和b非常重量级.即你无法复制它.
什么是完美的STL- way呢?如何使用std::sort来执行操作?做什么,如果a是const?
我目前做了什么:
#include <iostream>
#include <iomanip>
#include <type_traits>
#include <utility>
#include <iterator>
#include <algorithm>
#include <list>
#include <vector>
#include <cstdlib>
#include <cassert>
template< typename first, typename second >
void
sort_synchronously(first & f, second & s)
{
std::size_t sz = f.size();
assert(sz == s.size());
struct P …Run Code Online (Sandbox Code Playgroud) std::tuple是高度模板加载的野兽.要访问第n个成员编译器必须执行大量的模板实例化,尽管其性质简单:访问相应虚构结构的第n个数据成员.它似乎std::tuple应该是一个核心语言功能,像这样(伪代码):
template< typename ...types >
struct/* or class, or even union */ V
{
types... V; // defines implicitly `operator [/*constant expression*/]` to access by index
// if more than one variadic parameter pack provided
// (during expanding of parameter pack of template
// parameters in specializations) then instead of
// `V` there can be specific data-member name (say, `x`),
// but still with `x.operator []` implicitly defined
// member functions and data members allowed
};
template< …Run Code Online (Sandbox Code Playgroud) 是否可以使用结构化绑定语法确定在方括号中指定的变量名称数量,以匹配简单右侧的数据成员数量struct?
我想创建通用库的一部分,它使用结构化绑定将任意类分解为其组成部分.目前没有可变版本的结构化绑定(并且,我认为,不能用于当前提出的语法),但我首先想到的是对一些函数进行一组重载decompose(),它将struct参数分解为一组成分.decompose()应该通过参数的数量(即struct数据成员)来重载.目前constexpr if 语法也可以用来发送它.但是,我如何sizeof...为上述目的模拟类似于运算符的东西?我不能auto [a, b, c]在SFINAE结构中的某处使用语法,因为它是一个分解声明和AFAIK任何声明都不能在里面使用decltype,我也不能在lambda函数体中使用它,因为lambda函数也不能在模板参数中使用.
当然我想拥有内置运算符(语法类似于sizeof[] S/ sizeof[](S)for class S),但是类似下面的东西也是可以接受的:
template< typename type, typename = void >
struct sizeof_struct
{
};
template< typename type >
struct sizeof_struct< type, std::void_t< decltype([] { auto && [p1] = std::declval< type >(); void(p1); }) > >
: std::integral_constant< std::size_t, 1 >
{
};
template< typename type >
struct sizeof_struct< …Run Code Online (Sandbox Code Playgroud) c++ type-traits variadic-templates c++17 structured-bindings
要在本地执行一些递归任务,我使用以下方法来创建固定点组合器:
#include <utility>
#include <list>
#include <memory>
#include <iostream>
int main()
{
struct tree
{
int payload;
std::list< tree > children = {}; // std::list of incomplete type is allowed
};
std::size_t indent = 0;
// indication of result type here is essential
const auto print = [&] (const auto & self, const tree & node) -> void
{
std::cout << std::string(indent, ' ') << node.payload << '\n';
++indent;
for (const tree & t : node.children) {
self(self, t);
} …Run Code Online (Sandbox Code Playgroud) 如何以编程方式获取图形应用程序的屏幕截图?应用程序通过 DRM/KMS 使用 EGL API 绘制其窗口。
我使用 Ubuntu Server 16.04.3 和使用 Qt 5.9.2 和 EGLFS QPA 后端编写的图形应用程序。它从第一个虚拟终端(如果重要)开始,然后以全高清图形模式将显示切换到输出。
当我使用fb2png在 上运行的实用程序(例如)时/dev/fb?,只有第一个虚拟终端 ( Ctrl+Alt+F1) 的文本模式内容会保存为屏幕截图。
几乎没有 EGL API 可以从另一个进程的上下文中获取任何缓冲区的内容(这将是不安全的),但是也许有某种机制(和库)可以访问 GPU 的最终输出?