有没有理由让std::priority_queue构造函数通过常量引用接受比较器?如果比较器超出范围怎么办?
在@LightnessRacesInOrbit指出的可能移动比较器的情况下,我正在考虑这个问题!
如果已有关于此事的帖子,我很抱歉.我一直都找不到它!
有没有办法我只能在git子模块中提取最新的提交?我试图将boost作为git子模块放在一些项目中,但由于包含所有内容的boost repo非常重量级,所以我只想将子模块更新为最新的提交而不是拉出所有提交.这可能吗?
例如,当我这样做
git submodule update --init --recursive
Run Code Online (Sandbox Code Playgroud)
所有提升子模块都会被提交所有提交.我是否只能要求子模块镜像最新的提交而不是拉动所有更改?
注意具有该--depth标志的浅层克隆不起作用,因为它仅提取最新提交,并且最新提交仅包含在该提交中所做的更改,因此存储库未处于正确状态.
git archive当我尝试以下命令序列时,注意(如下面的答案所示)似乎不起作用
mkdir temp-git-test
cd temp-git-test
git init
git submodule add --depth 1 https://github.com/boostorg/boost
cd boost
git archive --format=tar HEAD --output ../boost.tar.gz
cd ..
tar -xzvf boost.tar.gz
Run Code Online (Sandbox Code Playgroud)
解压缩仓库的输出与子模块相同.难道我做错了什么?
当我依赖生命周期扩展来分配具有非平凡析构函数的类时,编译器(gcc和clang)都会发出未使用的变量警告.反正有没有绕过这个?https://wandbox.org/permlink/qURr4oliu90xJpqr
#include <iostream>
using std::cout;
using std::endl;
class Something {
public:
explicit Something(int a_in) : a{a_in} {}
Something() = delete;
Something(Something&&) = delete;
Something(const Something&) = delete;
Something& operator=(Something&&) = delete;
Something& operator=(const Something&) = delete;
~Something() {
cout << this->a << endl;
}
private:
int a;
};
int main() {
const auto& something = Something{1};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
请注意,当我切换到不依赖于生命周期扩展时,事情工作得很好https://wandbox.org/permlink/cJFwUDdi1YUEWllq
我甚至尝试手动定义所有构造函数,然后使用模板化删除它们,static_assert以便只有在调用这些构造函数时才触发它们https://wandbox.org/permlink/fjHJRKG9YW6VGOFb
#include <iostream>
using std::cout;
using std::endl;
template <typename Type>
constexpr auto definitely_false = false; …Run Code Online (Sandbox Code Playgroud) 为什么构造函数(4)std::variant来自http://en.cppreference.com/w/cpp/utility/variant/variant?似乎它会在代码中引起很多歧义,否则可以通过显式来避免.例如,cppreference上的代码示例突出显示用户可能不会注意到的可能的歧义(第三行)
variant<string> v("abc"); // OK
variant<string, string> w("abc"); // ill-formed, can't select the alternative to convert to
variant<string, bool> w("abc"); // OK, but chooses bool
Run Code Online (Sandbox Code Playgroud)
有些情况下绝对需要吗?
另一个问题是为什么需要从同一个cppreference页面构造函数(6)和(8).(5)和(7)不符合(6)和(8)的目的吗?我可能误解了他们的用法..
对于读者来说,我在问题中提到的构造函数是
constexpr variant(); // (1) (since C++17)
variant(const variant& other); // (2) (since C++17)
variant(variant&& other); // (3) (since C++17)
template< class T > // (4) (since C++17)
constexpr variant(T&& t);
template< class T, class... Args >
constexpr explicit variant(std::in_place_type_t<T>, Args&&... args); // (5) (since C++17)
template< class T, …Run Code Online (Sandbox Code Playgroud) 假设该体系结构可以以无锁的方式为std :: atomic支持8字节标量。为什么标准库不为8字节以下的结构提供类似的专业化知识?
这种std :: atomic专门化的简单实现可以std::memcpy将结构序列化/反序列化为等效形式std::uintx_t,其中xstruct的宽度以位为单位(四舍五入为最接近的2的幂,大于或等于2的整数)。结构的宽度)。这将很好地定义,因为std :: atomic要求这些结构是可微复制的。
例如。https://godbolt.org/z/sxSeId,这里Something只有3个字节,但实现调用__atomic_load和__atomic_exchange都使用锁表。
刚刚问了一个类似的问题,归结为这个问题.
#include <iostream>
using namespace std;
struct A {
A() : a{1} {};
int a;
};
template <typename Which>
struct WhichType;
int main() {
const A a;
const A& a_ref = a;
const A* a_ptr = &a;
WhichType<decltype(a.a)> which_obj; // template evaluates to int
WhichType<decltype(a_ref.a)> which_ref; // template evaluates to int
WhichType<decltype(a_ptr->a)> which_ptr; // template evaluates to int
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么模板不会成为const int代替int?
我需要对两个解决方案进行性能测试 - 一个使用多态来执行开启类型,另一个使用switch case来选择执行哪些函数.我真的需要优化这段代码.我编写了以下测试用例(您可以简单地复制粘贴代码,编译它g++ -std=c++14 -O3并运行它echo 1 | ./a.out!)如果您阅读它,代码非常简单!
#include <iostream>
#include <chrono>
#include <functional>
#include <array>
#include <cassert>
#include <vector>
#include <memory>
using namespace std;
struct profiler
{
std::string name;
std::chrono::high_resolution_clock::time_point p;
profiler(std::string const &n) :
name(n), p(std::chrono::high_resolution_clock::now()) { }
~profiler()
{
using dura = std::chrono::duration<double>;
auto d = std::chrono::high_resolution_clock::now() - p;
std::cout << name << ": "
<< std::chrono::duration_cast<dura>(d).count()
<< std::endl;
}
};
#define PROFILE_BLOCK(pbn) profiler _pfinstance(pbn)
class Base {
public:
virtual …Run Code Online (Sandbox Code Playgroud) 有没有办法可以使用using关键字为嵌套模板类设置别名?像这样的东西
template <typename... Types>
struct Something {
template <typename... TypesTwo>
struct Another {};
};
template <typename... Types>
template <typename... TypesTwo>
using Something_t = typename Something<Types...>::template Another<TypesTwo...>;
int main() {
Something_t<int><double>{};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个答案模板模板是嵌套模板的别名吗?显示了一种方法,但如果两个参数包都是可变参数,那么它将不再有效,因为编译器不知道从哪里开始以及在何处结束类型列表.
struct S {
static const int c;
};
const int d = 10 * S::c; // not a constant expression: S::c has no preceding
// initializer, this initialization happens after const
const int S::c = 5; // constant initialization, guaranteed to happen first
Run Code Online (Sandbox Code Playgroud)
为什么初始化d不是常量表达式(因此不是常量初始化过程的一部分)?注释似乎是因为它使用的值没有前面的初始值设定项,但似乎没有在条件列表中提及将表达式限定为常量表达式(这里列出了条件) .特别是哪些条件限定某些东西是一个常量表达式,它是否违反了?
如果它与必须在编译时评估常量初始化这一事实有关,那么标准提到恒定初始化不需要在编译时发生,甚至可能发生在例如加载时间.那为什么不只是c在编译时初始化而只是d在加载时?(我可能会把自己想成一个圈子)
感谢Jayesh我能够找到一个类似的问题"令人惊讶"的常量初始化,因为定义顺序,但答案似乎是谈论左值转换的左值,这里左值转换的左值是多少?除此之外,标准中没有引用关于这里违反了哪些条件.答案也没有解释为什么初始化不会分解为加载时和编译时.
我想知道最好问一下这样的问题,所以我在Meta(https://meta.stackexchange.com/questions/304981)上提出了这个问题并在这里进行了指导,所以这里有.
我非常好奇Google使用absl::Mutex(https://github.com/abseil/abseil-cpp/blob/master/absl/synchronization/mutex.h)对条件关键部分的实现进行了哪种优化.特别是我想知道当读者的情况变为现实时他们如何处理读者唤醒.他们是否也在等候名单中唤醒所有其他读者?这条线似乎表明他们确实这样做了.这不是每次都有O(n)遍历的风险,并且在写优先级互斥体中冒着雷鸣般的风险吗?