我有一个类模板:
template< typename ...bounded_types >
struct variant {};
Run Code Online (Sandbox Code Playgroud)
但是想禁止有界类型的空列表,即variant<>必须在编译时禁止.我可以做以下事情:
template<>
struct variant<>;
Run Code Online (Sandbox Code Playgroud)
但它不太清楚:如果我的变体库包含大量标题,那么它是不明显的,上面的特化是否不是类的前向声明,定义在下面的某处.在我看来,理想的想象解决方案将是:
template<>
struct variant<> = delete;
Run Code Online (Sandbox Code Playgroud)
这在很大程度上是明确的,但遗憾的是,C++语法禁止这样做.
什么是满足所述意图的最明确的方式?
标准中是否有一个子句描述了operator ()从基类调用的方法之间的以下区别?
#include <iostream>
#include <type_traits>
#include <cstdlib>
#include <cassert>
template< typename visitor, typename ...visitors >
struct composite_visitor
: std::decay_t< visitor >
, composite_visitor< visitors... >
{
//using std::decay_t< visitor >::operator ();
//using composite_visitor< visitors... >::operator ();
composite_visitor(visitor && _visitor, visitors &&... _visitors)
: std::decay_t< visitor >(std::forward< visitor >(_visitor))
, composite_visitor< visitors... >{std::forward< visitors >(_visitors)...}
{ ; }
};
template< typename visitor >
struct composite_visitor< visitor >
: std::decay_t< visitor >
{
//using std::decay_t< visitor >::operator ();
composite_visitor(visitor …Run Code Online (Sandbox Code Playgroud) 为什么我不能d在以下情况下明确指定?
#include <iostream>
template< typename a, typename b = int, typename ...c, typename d = int >
int
f(a, b, c..., d)
{
return sizeof...(c);
}
int
main()
{
std::cout << f< int, int, int, int/*, char*/ >(1, 2, 3, 4, 'd') << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我取消注释最后一个模板参数,那么我期望输出2,但我得到一个硬错误:
main.cpp:14:18: error: no matching function for call to 'f'
std::cout << f< int, int, int, int, char >(1, 2, 3, 4, 'd') << std::endl;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:6:1: note: candidate …Run Code Online (Sandbox Code Playgroud) 对于某些类来说S,重载一元operator +(或者可能operator *是非指针式类)如下所示是不好的做法?
struct S { S && operator + () & noexcept { return std::move(*this); } };
Run Code Online (Sandbox Code Playgroud)
它的目标是发明速记std::move.
S a;
S b = +a;
// instead of
S c = std::move(a);
Run Code Online (Sandbox Code Playgroud)
假设我有一个包含大量不同类的项目,它集中使用了移动语义.所有类都不模仿任何算术对应物.
c++ operator-overloading rvalue-reference move-semantics perfect-forwarding
如何在(例如)STL容器中引入聚合初始化的支持以正确构建它们?我的意思是:
struct A { int i; char c; };
std::list< A > l; // empty
l.insert(std::memberwise, 1, '2');
// <=> l.insert({1, '2'});
Run Code Online (Sandbox Code Playgroud)
std::memberwise是一个可能的标签,就像在STL中已经存在一样std::piecewise_construct,std::allocator_arg等等.
从理论上讲,以这种方式扩展STL容器是否可行?有没有最好的方法("STL-way")来做到这一点?它看起来怎么样?
问题是关于界面设计和(内部)实现的可能性(不是细节).
我确定容器使用::new (static_cast< void * >(pstorage) value_type(std::forward< Args >(args)...);内部的东西.我确定,用括号替换括号会破坏更改.因为非缩小,例如
l.insert({1, '2'});评论中一般提到可能会导致过度移动value_type.很可能这一步将由任何现代编译器优化,但无论哪种方式都有过多的花括号.
我想动态重新翻译Qt Quick GUI字符串.
重新创建受影响的字符串属性存在侵入性技巧,其中有关更改的通知无法集中.
是否有可能使qsTr(和其他)string类似返回的对象,其行为完全相同string,但行为类似于连接到常见"valueChanged"信号的全局属性(我想QEvent::LanguageChange在QCoreApplication发生时发出它).
我想我可以使用twitching Loader的active属性,它包含整个顶级GUI元素,使所有用户可见的字符串重新翻译,但这种方法导致所有项目和组件的状态丢失,连接到Loader并且没有差异从完整的应用程序重启为我.
是否有可能创造这样的myQsTr功能?
英特尔®64和IA-32体系结构软件开发人员手册(第2卷)表示,对于FST / FSTP FPU指令F 受影响的标志:
简单的测试(几乎没有任何价值)告诉我,C0,C2,C3不会受到影响:
#include <iostream>
#include <bitset>
#include <cstdlib>
#include <cstdint>
int main()
{
double x = -1.0;
std::uint16_t a = 0, b = 0;
asm volatile ("fld %[x] ; ftst ; fnstsw %%ax ; mov %%ax, %[a] ; fstp %%st ; fnstsw %%ax ; mov %%ax, %[b] ;"
: [a]"=m"(a), [b]"=m"(b)
: [x]"t"(x)
: "cc", "memory");
std::cout << std::bitset< 16 >(a) << std::endl;
std::cout << std::bitset< 16 …Run Code Online (Sandbox Code Playgroud) 我试图找到种子来散列最大可能长度的小写字母短字符串而不会发生冲突。我选择了 SSE 4.2 CRC32 来简化任务。对于长度为 4、5、6 的种子,在一些合理的小值内没有碰撞(我不能无限等待)。
#include <bitset>
#include <limits>
#include <iterator>
#include <iostream>
#include <x86intrin.h>
static std::bitset<size_t(std::numeric_limits<uint32_t>::max()) + 1> hashes;
static void findSeed()
{
uint8_t c[7];
const auto findCollision = [&] (uint32_t seed)
{
std::cout << "seed = " << seed << std::endl;
hashes.reset();
for (c[0] = 'a'; c[0] <= 'z'; ++c[0]) {
uint32_t hash0 = _mm_crc32_u8(~seed, c[0]);
for (c[1] = 'a'; c[1] <= 'z'; ++c[1]) {
uint32_t hash1 = _mm_crc32_u8(hash0, c[1]);
for (c[2] = 'a'; c[2] <= …Run Code Online (Sandbox Code Playgroud) 为什么this在静态成员函数中不允许未评估的上下文?
struct A
{
void f() {}
static void callback(void * self) // passed to C function
{
static_cast< decltype(this) >(self)->f();
}
};
Run Code Online (Sandbox Code Playgroud)
此代码给出错误:
错误:'this'不适用于静态成员函数
Run Code Online (Sandbox Code Playgroud)static_cast< decltype(this) >(self)->f(); ^~~~
decltype(this)为了简洁需要(有时它会短得多VeryVeryLongClassName *),另一个优点是意图更清晰.
标准说什么this在静态成员函数中使用未评估的上下文?
QT的对口有序关联容器std::map是QMap,std::set是QSet,无序关联容器std::unordered_map是QHash.
std::unordered_set在Qt中我应该用什么来代替?没有QHash< T, void >专业化,也没有QHash< T >.
有计划的模拟吗?