小编She*_*rei的帖子

类字符串对象的概念

标准库中是否有类似字符串对象的概念?我的意思是这样的

template<typename T>
concept StringLike = requires(T x) {
   /* is same as const char*, char*, std::string etc */
}
Run Code Online (Sandbox Code Playgroud)

有一个关于 字符串的手工 Type 特征的问题。但我找不到任何有关概念的信息。为什么它不包含在 std 中?

c++ c++-concepts c++20

6
推荐指数
1
解决办法
2152
查看次数

std::lexicographyal_compare_third_way 用于范围

为什么有std::lexicographical_compare_three_way,却没有std::ranges::lexicographical_compare_three_way

中有参数 Comp std::ranges::lexicographical_compare,但它相当无用,因为bool当需要比较类别类型之一时,函数返回 。

以下是 cppref 的一些链接
https://en.cppreference.com/w/cpp/algorithm/lexicographyal_compare_ Three_way
https://en.cppreference.com/w/cpp/algorithm/ranges/lexicographyal_compare

c++ c++20 std-ranges

5
推荐指数
1
解决办法
368
查看次数

在 constexpr 函数中放置 new 和 delete

假设我们有一些课程

struct Foo {
    constexpr Foo(int& x) : x_(x) { x_++; }
    constexpr ~Foo() noexcept     { x_++; }

    int& x_;
};
Run Code Online (Sandbox Code Playgroud)

在 C++20 中,g++-10 -std=c++20我们可以有这样的函数:

constexpr int do_foo_1() {
    int x = 0;
    Foo* p = new Foo(x);
    delete p;
    return x;
}

int main() {
    static_assert(do_foo_1() == 2);
}
Run Code Online (Sandbox Code Playgroud)

让我们尝试将运算符new分为内存分配和就地构造。所以新函数看起来像这样:

constexpr int do_foo_2() {
    int x = 0;
    Foo* p = static_cast<Foo*>(::operator new(sizeof(Foo)));
    p = ::new ((void*) p) Foo(x);
    p->~Foo();
    ::operator delete(p);
    return x;
} …
Run Code Online (Sandbox Code Playgroud)

c++ c++20

5
推荐指数
1
解决办法
131
查看次数

使 iostream 更加严格

有没有办法通过一些标志使 iostreams 对布尔值更加严格?

我使用得到了意想不到的结果std::boolalpha

bool var;
std::istringstream is("true1");
is >> std::boolalpha >> var;
Run Code Online (Sandbox Code Playgroud)

产量var == true,is.good() == 1is.peek() == '1', 当我预期时is.good() == 0

当没有指定 boolalpha 时类似的行为:

bool var;
std::istringstream is("1qwe");
is >> var;
Run Code Online (Sandbox Code Playgroud)

产量var == true,is.good() == 1is.peek() == 'q', 当我预期时is.good() == 0

c++

1
推荐指数
1
解决办法
70
查看次数

标签 统计

c++ ×4

c++20 ×3

c++-concepts ×1

std-ranges ×1