我有以下关于 C++20 中的 range 库的问题:
让std::ranges::contiguous_range<T>一个任意类型T.
我可以假设std::ranges::sized_range<T>吗?
请考虑以下代码
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) 下面的程序
#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 的行为就在这里。
那是对的吗?
在 fputc()/putc() 上阅读en.cppreference.com,我偶然发现了以下语句:
在 C 中,putc() 可以作为宏实现,这在 C++ 中是不允许的。
这是真的?如果是这样,这是在哪里(在 C++ 标准中)说明的?
我在以下程序中遇到 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++ ×5
c++17 ×3
c++20 ×1
constructor ×1
gcc ×1
iterator ×1
stdio ×1
stl ×1
template-argument-deduction ×1
templates ×1
type-traits ×1