该位摆弄黑客网站提出以下建议非常有效的功能,以扭转位:
// Bitswap: reverse the bits of the value of unsigned integral type T
template <class T>
constexpr T bitswap(T src)
{
constexpr std::size_t char_bit = std::numeric_limits<unsigned char>::digits;
constexpr std::size_t digits = sizeof(T) * char_bit;
std::size_t size = digits;
T mask = ~T();
while ((size >>= 1) > 0) {
mask ^= (mask << size);
src = ((src >> size) & mask) | ((src << size) & ~mask);
}
return src;
}
Run Code Online (Sandbox Code Playgroud)
__uint128_t?(原始版本适用__uint128_t) …考虑以下代码:
#include <bitset>
#include <sstream>
#include <iostream>
int main(int argc, char* argv[])
{
std::stringstream stream;
std::bitset<1> bitset(1);
std::cout<<"before = "<<bitset[0]<<std::endl;
stream<<"4";
stream>>bitset;
std::cout<<"after = "<<bitset[0]<<std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
g++在下编译libstdc++,结果为:
> g++ bitset_empty.cpp -o bitset_empty
> ./bitset_empty
before = 1
after = 1
Run Code Online (Sandbox Code Playgroud)
clang++在下编译libc++,结果为:
> clang++ -stdlib=libc++ bitset_empty.cpp -o bitset_empty
> ./bitset_empty
before = 1
after = 0
Run Code Online (Sandbox Code Playgroud)
哪一个是正确的?两者(因为未定义的行为)?海湾合作委员会?铛?
// Include
#include <tuple>
#include <iostream>
#include <type_traits>
// Temporary function queue declaration
template <class... F>
class temporary_function_queue;
// Apply function queue declaration
template <class... F>
constexpr temporary_function_queue<F&&...> apply_function_queue(F&&... f);
// Temporary function queue definition
template <class... F>
class temporary_function_queue final
{
// Types
private:
using const_lvalue_reference = const temporary_function_queue&;
using rvalue_reference = temporary_function_queue&&;
using temporary_type = temporary_function_queue<F&&...>;
using data_type = std::tuple<F&&...>;
// Lifecycle
private:
temporary_function_queue(rvalue_reference) = default;
temporary_function_queue(const_lvalue_reference) = delete;
temporary_function_queue …Run Code Online (Sandbox Code Playgroud) c++ templates template-meta-programming perfect-forwarding c++14
考虑下面的代码,这些代码是受Barry对这个问题的回答启发而来的:
// Include
#include <tuple>
#include <utility>
#include <iostream>
#include <type_traits>
// Generic overload rank
template <std::size_t N>
struct overload_rank
: overload_rank<N - 1>
{
};
// Default overload rank
template <>
struct overload_rank<0>
{
};
// Prepend argument to function
template <std::size_t N, class F>
auto prepend_overload_rank(F&& f) {
using rank = overload_rank<N>;
return [f = std::forward<F>(f)](rank, auto&&... args) -> decltype(auto) {
return std::forward<F>(f)(std::forward<decltype(args)>(args)...); // here
};
}
// Main
int main(int argc, char* argv[])
{
auto …Run Code Online (Sandbox Code Playgroud) 对于类型为T的类,编译器可以生成以下成员,具体取决于类:
T::T()T::T(const T&)T::T(T&&)T& T::operator=(const T&)T& T::operator=(T&&)在C++ 14和C++ 17中,哪些规则导致constexpr编译器生成这些函数的版本?
我想了解推论指南如何与通用引用配合使用std::forward,尤其是创建完美的转发包装器。下面的代码提供了一种在两种情况下使用函子包装器进行实验的代码:一种具有隐式推导指南,另一种具有显式推导指南。
我已经把的很多&&,并std::forward在评论,因为我不知道他们需要的地方,实现完美转发。我想知道将它们放在哪里,不需要它们。
// Case with not conversion constructor
template <class F>
struct functor1
{
explicit constexpr functor1(F/*&&*/ f)
noexcept(std::is_nothrow_copy_constructible_v<F/*&&*/>)
: _f(/*std::forward<F>(*/f/*)*/)
{}
template <class... Args>
constexpr operator()(Args&&... args)
noexcept(std::is_nothrow_invocable_v<F/*&&*/, Args/*&&*/...>)
{
/*std::forward<F>(*/_f/*)*/(std::forward<Args>(args)...);
}
private: F/*&&*/ _f;
};
// Case with a conversion constructor
template <class F>
struct functor2
{
template <class G>
explicit constexpr functor2(G&& g)
noexcept(std::is_nothrow_constructible_v<G/*&&*/, F/*&&*/>)
: _f(/*std::forward<G>(*/g/*)*/)
{}
template <class... Args>
constexpr operator()(Args&&... args)
noexcept(std::is_nothrow_invocable_v<F/*&&*/, Args/*&&*/...>)
{
/*std::forward<F>(*/_f/*)*/(std::forward<Args>(args)...);
}
private: F/*&&*/ …Run Code Online (Sandbox Code Playgroud) c++ templates perfect-forwarding template-argument-deduction c++17
std::chrono提供几个时钟来测量时间。同时,我猜 cpu 评估时间的唯一方法是计数周期。
问题 1: cpu 或 gpu 是否可以通过计数周期来评估时间?
如果是这样的话,因为计算机计数周期的方式永远不会像原子钟那样精确,这意味着period = std::ratio<1>计算机的“秒”()实际上可能比实际秒更短或更大,从而导致在长时间运行计算机时钟和 GPS 之间的时间测量。
问题2:正确吗?
某些硬件具有不同的频率(例如空闲模式和 Turbo 模式)。在这种情况下,这意味着循环数将在一秒钟内发生变化。
问题 3: cpu 和 gpus 测量的“周期数”是否因硬件频率而异?如果是,那么如何std::chrono处理?如果不是,一个周期对应什么(比如什么是“基本”时间)?有没有办法在编译时访问转换?有没有办法在运行时访问转换?
考虑以下高度模板化的代码:
// Preamble
#include <list>
#include <deque>
#include <vector>
#include <iostream>
#include <type_traits>
// Rebind template template type traits
template <class> struct rebind_template_template;
template <template <class...> class Template, class... Types>
struct rebind_template_template<Template<Types...>> {
template <class... Args>
using type = Template<Args...>;
};
// Rebind template parameters type traits
template <class> struct rebind_template_parameters;
template <template <class...> class Template, class... Types>
struct rebind_template_parameters<Template<Types...>> {
template <template <class...> class Arg>
using type = Arg<Types...>;
};
// Template pack
template <template <class...> class... Templates>
class …Run Code Online (Sandbox Code Playgroud) c++ template-templates compiler-bug template-argument-deduction c++17
我想知道是否有一个技巧可以简化特征的编写以返回类型是否为noexcept 函数。目前我的实现如下,它只是一一列出所有可能性。可以使用标准以更简单的方式编写C++20吗?
// Default
template <class>
struct is_noexcept_function: std::false_type {};
// Variable template
template <class T>
inline constexpr bool is_noexcept_function_v
= is_noexcept_function<T>::value;
// Noexcept functions
template <class R, class... Args>
struct is_noexcept_function<R(Args...) noexcept>: std::true_type {};
template <class R, class... Args>
struct is_noexcept_function<R(Args...) const noexcept>: std::true_type {};
template <class R, class... Args>
struct is_noexcept_function<R(Args...) volatile noexcept>: std::true_type {};
template <class R, class... Args>
struct is_noexcept_function<R(Args...) const volatile noexcept>: std::true_type {};
// Noexcept lvalue-ref-qualified functions
template <class R, …Run Code Online (Sandbox Code Playgroud) 在许多cmake find模块中,我们发现INCLUDE("${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake")
我想做同样的事情,但是来自我项目的CMakeLists.问题是${CMAKE_CURRENT_LIST_DIR}指向我项目的目录......并且FindPackageHandleStandardArgs.cmake正在进行中/usr/share/cmake-2.8/Modules/.
是否有一个指向cmake模块目录的CMake变量?如果没有,如何在我的CMakeLists中做到这一点(我希望它是可移植的而不是"硬编码")?
非常感谢你.
c++ ×8
c++14 ×4
c++17 ×3
templates ×3
template-argument-deduction ×2
benchmarking ×1
bit ×1
c++-chrono ×1
c++20 ×1
clang ×1
cmake ×1
compiler-bug ×1
constexpr ×1
constructor ×1
cpu ×1
function ×1
gcc ×1
lambda ×1
module ×1
noexcept ×1
recursion ×1
std ×1
std-bitset ×1
time ×1