取自 GCC 实现type_traits
为什么static_cast
需要这里?
template <typename _Tp, typename... _Args>
struct __is_nt_constructible_impl
: public integral_constant<bool, noexcept(_Tp(declval<_Args>()...))> {};
template <typename _Tp, typename _Arg>
struct __is_nt_constructible_impl<_Tp, _Arg>
: public integral_constant<bool,
// Why is `static_cast` needed here?
noexcept(static_cast<_Tp>(declval<_Arg>()))> {};
Run Code Online (Sandbox Code Playgroud) 在搜索 C++ 中的文件读取示例时,我注意到许多示例使用
std::ios::binary
与std::ifstream::binary
std::ios::beg
与your_file_stream.beg
std::ios::end
与your_file_stream.end
这些示例有什么区别吗?如果没有,为什么它们都包含在 STL 中?
我工作的地方目前正在制作 PWA 应用程序。该应用程序在 Android、桌面 Chrome 和桌面 Safari 上运行良好。内存方面需要 16 到 18mb。
但是在 iOS 上,加载过多图像后,应用程序崩溃并重新加载页面。我已经尝试搜索 anwsers。我尝试通过从 blob 创建 URL 来缓存图像以尝试减少重复,但应用程序仍然崩溃。(这些图像与主缓存的处理方式不同。它们不需要离线可用)不仅如此,而且该页面似乎比在 Android 上占用的内存更多——在我的 iPhone 上,它显示为 38mb。
所有这些图像都是从 Ajax 请求中获取的,然后其中一些显示在可滚动列表中,而另一些则显示在不同的上下文中。
我已经读到 Mobile Safari 在整个页面上有 10mb 的限制。我们的应用程序仅由一个分为多个部分的网页组成,它是使用 jQuery Mobile 创建的,其大部分内容都是动态生成的。
你们中是否有人遇到过类似的问题,如果有,你们是如何解决的?
在 C 中,在实现文件内,当在同一文件中前向声明静态函数时,函数的声明(原型)和定义中是否都需要 static 关键字?
下面的情况是std::move
多余的吗?
std::string member;
obj(std::initializer_list<std::string> p_list)
: member {std::move(join(p_list))}
{}
Run Code Online (Sandbox Code Playgroud)
这是连接函数:
std::string join(string_initializer_list p_list) {
size_t size {};
for (auto const & s : p_list) {
size += s.size();
}
std::string output;
output.reserve(size);
for (auto const & s : p_list) {
output.append(s);
}
return output;
}
Run Code Online (Sandbox Code Playgroud) 在使用C ++进行原型设计和玩耍时,尝试制作可识别utf8的不可变字符串的一些概念,但我遇到了以下难题:
有什么方法可以返回字符串的不变视图。就像,而不是返回子字符串,我希望能够返回引用原始字符串一部分的子字符串。
// Just some quick prototyping of ideas.
// Heavier than just a normal string.
// Construction would be heavier too because of the indices vector.
// Size would end up being O1 though.
// Indexing would also be faster.
struct ustring {
std::string data;
std::vector<size_t> indices;
// How do I return a view to a string?
std::string operator [](size_t const i) const {
return data.substr(indices[i], indices[i + 1] - indices[i]);
}
};
Run Code Online (Sandbox Code Playgroud) 这是 C++20 中的标准行为吗?我在 cppreference 中找不到任何关于它的信息。
我刚刚在 Clang 和 Visual Studio 上都尝试过,它可以工作,并且没有给我任何错误或警告。我还检查了调试器,看看是否operator==
被调用,它是!C++20 现在是否允许自动生成operator!=
何时operator==
存在?它默认为 sane!(a == b)
吗?如果是这样,那对 C++ 来说真是太棒了!
c++ comparison operator-overloading comparison-operators c++20
在 Linux/POSIX 上相当于
TerminateProcess(GetCurrentProcess(), 0)
Run Code Online (Sandbox Code Playgroud)
我希望能够突然、无序地终止程序,无论我是否在线程中。我不关心析构函数、退出处理程序或优雅终止,我只想完全关闭/终止程序。
我必须包含哪些系统库以及必须使用哪些函数/参数?
以下示例在所有主要编译器中均失败:clang
、gcc
和visual studio
。
我想知道它出了什么问题,看起来很简单:
如果sizeof...(TYPES) == 2
那么它应该排除一个重载并接受另一个重载,如果0、1或大于2那么它应该接受第一个重载并排除第二个重载。
为什么这行不通呢?
#include <iostream>
#include <type_traits>
template <typename... TYPES>
struct Test {
template <std::enable_if_t<(sizeof...(TYPES) != 2), int> = 0>
Test() {
std::cout << "A\n";
}
template <std::enable_if_t<(sizeof...(TYPES) == 2), int> = 0>
Test() {
std::cout << "B\n";
}
};
int
main() {
Test<int, int> t1;
Test<int, int, int> t2;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
clang
错误:
In file included from C:\Users\joaopires\Dropbox\++A\tests\par_pack.cpp:1:
In file included from C:\Program Files (x86)\Microsoft Visual …
Run Code Online (Sandbox Code Playgroud) 什么是明确的最好,最快,最可靠的方法std::list
和std::set
?
// 1st
list.clear();
set.clear();
// 2nd
list = {};
set = {};
// 3rd
list.swap(std::list<some_type>{});
set.swap(std::set<some_type>{});
Run Code Online (Sandbox Code Playgroud)