小编ple*_*ndo的帖子

contiguous_range 总是一个 sized_range 吗?

我有以下关于 C++20 中的 range 库的问题:

std::ranges::contiguous_range<T>一个任意类型T.

我可以假设std::ranges::sized_range<T>吗?

c++ iterator stl language-lawyer c++20

22
推荐指数
3
解决办法
824
查看次数

构造函数中的完美转发(C++ 17)

请考虑以下代码

struct A {
    A(int id) : id_ { id } {}

    A(const A& rhs) { std::cout << "cctor from " +
        std::to_string(rhs.id_) << std::endl; }
    A(A&& rhs) { std::cout << "mctor from " +
        std::to_string(rhs.id_) << std::endl; }

    int id_;
};

template<typename T>
struct B1 {
    constexpr B1(T&& x) noexcept : x_ { std::forward<T>(x) } {}

    T x_;
};

template<typename T>
struct B2 {
    constexpr B2(T&& x) noexcept;

    T x_;
};

template<typename T>
constexpr
B2<T>::B2(
    T&& x
) noexcept …
Run Code Online (Sandbox Code Playgroud)

c++ gcc language-lawyer template-argument-deduction c++17

13
推荐指数
2
解决办法
1279
查看次数

std::initializer_list&lt;std::string_view&gt; 的初始化

下面的程序

#include <initializer_list>
#include <string_view>

inline constexpr std::initializer_list<std::string_view> s = { "" };

int main() {}
Run Code Online (Sandbox Code Playgroud)

使用当前的 Clang (12.0.0) 编译,但不使用当前的 GCC (11.0.0 20201028) 编译。使用 GCC 会产生错误

prog.cc:4:67: error: modification of '<temporary>' is not a constant expression
    4 | inline constexpr std::initializer_list<std::string_view> s = { "" };
      |                                                                    ^
Run Code Online (Sandbox Code Playgroud)

来自[dcl.init.list/5]以及string_view(char const*)构造函数是constexpr的事实,我假设 Clang 的行为就在这里。

那是对的吗?

c++ constructor language-lawyer c++17

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

Why can't putc() be implemented as a macro in C++? (Or can it?)

在 fputc()/putc() 上阅读en.cppreference.com,我偶然发现了以下语句:

在 C 中,putc() 可以作为宏实现,这在 C++ 中是不允许的。

这是真的?如果是这样,这是在哪里(在 C++ 标准中)说明的?

可能相关:https : //stackoverflow.com/questions/10712423

c++ stdio language-lawyer

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

std::is_invocable 的奇怪行为

我在以下程序中遇到 std::is_invocable 问题:

#include <iostream>
#include <type_traits>

void g() {}

template<bool B, typename T>
void f(T t) {
    if constexpr (B)
        g(t);
}

int main() {
    std::cerr << std::boolalpha <<
        std::is_invocable_v<decltype(&f<true, int>), int> << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我原以为程序输出为假,因为 f<true, int> 无法实例化。但是,GCC(从 10.0.1 20200224 开始)不会编译,错误消息为

test.cpp: In instantiation of 'void f(T) [with bool B = true; T = int]':
test.cpp:14:51:   required from here
test.cpp:9:10: error: too many arguments to function 'void g()'
    9 |         g(t);
      |         ~^~~
test.cpp:4:6: note: declared …
Run Code Online (Sandbox Code Playgroud)

c++ templates type-traits language-lawyer c++17

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