在 C++14 中,由于constexpr不再隐式const,constexpr成员函数可以修改类的数据成员:
struct myclass
{
int member;
constexpr myclass(int input): member(input) {}
constexpr void f() {member = 42;} // Is it allowed?
};
Run Code Online (Sandbox Code Playgroud) 在C ++中,标准和平台无关性(无论如何签名)都可以很好地定义将有符号整数值转换为可以有两种不同大小(例如:short intto unsigned long long int或long long intto unsigned char)的无符号整数值的结果。例如代表整数)?
SHLD/SHRD指令是用于实现多精度移位的汇编指令.
请考虑以下问题:
uint64_t array[4] = {/*something*/};
left_shift(array, 172);
right_shift(array, 172);
Run Code Online (Sandbox Code Playgroud)
什么是实行最有效的方法left_shift和right_shift,经营4个64位无符号整数数组上的转变,就好像它是一个巨大的256位无符号整数两种功能?
最有效的方法是使用SHLD/SHRD指令,还是有更好的(如SIMD版本)现代架构指令?
请考虑以下代码:
#include <limits>
#include <cstdint>
using T = uint32_t; // or uint64_t
T shift(T x, T y, T n)
{
return (x >> n) | (y << (std::numeric_limits<T>::digits - n));
}
Run Code Online (Sandbox Code Playgroud)
根据godbolt,clang 3.8.1 为-O1,-O2,-O3生成以下汇编代码:
shift(unsigned int, unsigned int, unsigned int):
movb %dl, %cl
shrdl %cl, %esi, %edi
movl %edi, %eax
retq
Run Code Online (Sandbox Code Playgroud)
而gcc 6.2(即使有-mtune=haswell)生成:
shift(unsigned int, unsigned int, unsigned int):
movl $32, %ecx
subl %edx, %ecx
sall %cl, %esi
movl %edx, %ecx
shrl %cl, %edi …Run Code Online (Sandbox Code Playgroud) lambdas 没有默认构造函数的原因是什么?这背后是否有任何技术原因,还是纯粹的设计决定?
在cppreference上,写道正确的使用方法std::result_of是:
template<class F, class... Args>
std::result_of_t<F&&(Args&&...)>
// instead of std::result_of_t<F(Args...)>, which is wrong
my_invoke(F&& f, Args&&... args) {
/* implementation */
}
Run Code Online (Sandbox Code Playgroud)
我想知道std::invoke_result_t应该如何使用
invoke_result_t:
template<class F, class... Args>
std::invoke_result_t<F&&, Args&&...> my_invoke(F&& f, Args&&... args);
Run Code Online (Sandbox Code Playgroud)
要么:
template<class F, class... Args>
std::invoke_result_t<F, Args...> my_invoke(F&& f, Args&&... args);
Run Code Online (Sandbox Code Playgroud) 考虑以下代码,其中一个仿函数derived继承自两个基类base1,base2每个基类提供不同的重载:
// Preamble
#include <iostream>
#include <functional>
#include <type_traits>
// Base 1
struct base1 {
void operator()(int) const {
std::cout << "void base1::operator()(int) const\n";
}
void operator()(double) const {
std::cout << "void base1::operator()(double) const\n";
}
template <class Arg, class... Args>
void operator()(const Arg&, Args&&...) const {
std::cout << "void base1::operator()(const Arg&, Args&&...) const\n";
}
};
// Base 2
struct base2 {
void operator()(int) {
std::cout << "void base2::operator()(int)\n";
}
void operator()(double) {
std::cout << …Run Code Online (Sandbox Code Playgroud) 考虑C ++标准库中的以下算法:std::shuffle该算法具有以下签名:
template <class RandomIt, class URBG>
void shuffle(RandomIt first, RandomIt last, URBG&& g);
Run Code Online (Sandbox Code Playgroud)
它对给定范围内的元素进行重新排序,以使[first, last)这些元素的每个可能排列具有相同的出现概率。
我正在尝试实现相同的算法,但是它在位级别起作用,随机地对输入序列的单词的位进行改组。考虑到64位字的序列,我正在尝试实现:
template <class URBG>
void bit_shuffle(std::uint64_t* first, std::uint64_t* last, URBG&& g)
Run Code Online (Sandbox Code Playgroud)
问题:如何尽可能有效地做到这一点(必要时使用编译器内部函数)?我并不一定要寻找一个完整的实现,而是要寻找更多的建议/研究方向,因为对于我来说,实际上是否有效地实现它尚不明确。
我目前正在将使用qmake构建的项目转移到CMake.
在带有qmake的版本中,在.pri文件中,有
MOC_DIR = .moc/$${PLATFORM_NAME}
Run Code Online (Sandbox Code Playgroud)
允许在给定目录中生成MOC临时文件,保持源清洁.如何用CMake做同样的事情?
注意:使用CMake,我使用FindQt4.cmake包和命令QT4_WRAP_CPP().
我有一个来自英特尔的makefile,其中有一些"?=".喜欢
COMPILER ?= $(GCC_PATH)g++
Run Code Online (Sandbox Code Playgroud)
但
EXECUTABLE = run
Run Code Online (Sandbox Code Playgroud)
有什么区别?=和=我何时必须使用第一个而不是第二个?
非常感谢你.