在CPP参考文档中,
我注意到了 char
字符类型足够大,可以表示任何UTF-8八位代码单元(自C ++ 14起)
和为 char8_t
UTF-8字符表示形式的类型,必须足够大以表示任何UTF-8代码单元(8位)
这是否意味着两者是同一类型?还是char8_t有其他功能?
当显式默认析构函数时(例如:)struct s { ~s() = default; };,似乎该类型仍然可以在constexpr上下文中使用。但是,当显式删除析构函数(例如:)时struct s { ~s() = delete; };,Clang 不再认为该类型对于constexpr上下文是可行的。那么,正如标题所暗示的那样,具有显式删除的析构函数的类型仍然可以在上下文中使用吗constexpr?
struct a { ~a() = default; }; // all ok
struct b { ~b(){} }; // all fail
struct c { ~c() = delete; }; // fails on clang
struct d { constexpr ~d() = default; }; // all ok
struct e { constexpr ~e(){} }; // all ok
struct f { …Run Code Online (Sandbox Code Playgroud) 显然,consteval它将成为C++ 20中的关键字.它的cppreference页面目前是空白的.它会是什么以及它与它有什么关系constexpr?
#include <ranges>
#include <iostream>
#include <string_view>
using namespace std::literals;
int main()
{
auto fn_is_l = [](auto const c) { return c == 'l'; };
{
auto v = "hello"sv | std::views::filter(fn_is_l);
std::cout << *v.begin() << std::endl; // ok
}
{
auto const v = "hello"sv | std::views::filter(fn_is_l);
std::cout << *v.begin() << std::endl; // error
}
}
Run Code Online (Sandbox Code Playgroud)
请参阅:https : //godbolt.org/z/vovvT19a5
<source>:18:30: error: passing 'const std::ranges::filter_view<
std::basic_string_view<char>, main()::
<lambda(auto:15)> >' as 'this' argument discards
qualifiers [-fpermissive]
18 | std::cout << *v.begin() …Run Code Online (Sandbox Code Playgroud) 显然,constexpr std::string尚未添加到GCC的libstdc++中(从 GCC v11.2 开始)。
这段代码:
#include <iostream>
#include <string>
int main()
{
constexpr std::string str { "Where is the constexpr std::string support?"};
std::cout << str << '\n';
}
Run Code Online (Sandbox Code Playgroud)
不编译:
#include <iostream>
#include <string>
int main()
{
constexpr std::string str { "Where is the constexpr std::string support?"};
std::cout << str << '\n';
}
Run Code Online (Sandbox Code Playgroud)
char当字符串包含超过 16秒(因为 GCC 的 SSO 缓冲区大小为 16)时,此类字符串将如何工作?简短的解释是什么?一个简单的构造函数会在堆栈上创建字符串对象并且从不使用动态分配吗?
这段代码:
std::cout << "is_trivially_constructible: "
<< std::boolalpha << std::is_trivially_constructible<const std::string>::value << '\n'; …Run Code Online (Sandbox Code Playgroud) 与问题相关如何检查C++ 11支持?而什么是__cplusplus对C++ 17的价值?
如何查询编译器是否可以处理/设置为使用C++ 20?我知道原则上可以通过以下方式查询C++版本:
#if __cplusplus > ???
// C++20 code here
#endif
Run Code Online (Sandbox Code Playgroud)
什么应该???是C++ 20?
我的代码遇到了瓶颈,所以这个问题的主要问题是性能。
我有一个十六进制校验和,我想检查一个字符数组的前导零。这就是我正在做的:
bool starts_with (char* cksum_hex, int n_zero) {
bool flag {true};
for (int i=0; i<n_zero; ++i)
flag &= (cksum_hex[i]=='0');
return flag;
}
Run Code Online (Sandbox Code Playgroud)
如果cksum_hex有,则上述函数返回truen_zero前导零,。但是,对于我的应用程序,此功能非常昂贵(占总时间的 60%)。换句话说,它是我代码的瓶颈。所以我需要改进它。
我还检查了std::string::starts_with哪些在 C++20 中可用,我发现性能没有差异:
// I have to convert cksum to string
std::string cksum_hex_s (cksum_hex);
cksum_hex_s.starts_with("000"); // checking for 3 leading zeros
Run Code Online (Sandbox Code Playgroud)
有关我正在使用的更多信息 g++ -O3 -std=c++2a,我的 gcc 版本是 9.3.1。
std::string::starts_with?考虑函数模板的以下两个重载foo:
template <typename T>
void foo(T) requires std::integral<T> {
std::cout << "foo requires integral\n";
}
template <typename T>
int foo(T) requires std::integral<T> && true {
std::cout << "foo requires integral and true\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
请注意两个约束之间的区别:第二个约束有一个额外的&& true.
直观地说,true在连词中是多余的(因为X && trueis just X)。然而,它看起来像这使得语义差别,如foo(42)将调用第二个过载。
为什么会这样?具体来说,为什么第二个函数模板是更好的重载?
#include <type_traits>
int main()
{
auto f1 = [](auto&) mutable {};
static_assert(std::is_invocable_v<decltype(f1), int&>); // ok
auto const f2 = [](auto&) {};
static_assert(std::is_invocable_v<decltype(f2), int&>); // ok
auto const f3 = [](auto&) mutable {};
static_assert(std::is_invocable_v<decltype(f3), int&>); // failed
}
Run Code Online (Sandbox Code Playgroud)
查看演示
为什么 const mutable lambda 不能采用引用参数?
c++ ×10
c++20 ×10
c++-concepts ×1
c++-modules ×1
c++14 ×1
constants ×1
consteval ×1
constexpr ×1
lambda ×1
macros ×1
optimization ×1
performance ×1
standards ×1
std-ranges ×1
stdstring ×1
type-traits ×1