根据 C++ 标准草案,执行std::iter_value_t必须是:
\n\n使用 iter_value_t 的模板=见下文;
\n让 RI 成为
\nremove_cvref_t<I>。类型iter_value_t<I>表示(2.1)\xe2\x80\x94
\nindirectly_readable_traits<RI >::value_type如果iterator_traits<RI>命名从主模板生成的专业化,并且(2.2)\xe2\x80\x94
\niterator_traits<RI >::value_type否则。
我明白那个:
\ntype iter_value<T>: // OK, this is not C++, is to clarify my question\n type R = remove_cvref_t<T>;\n if (iterator_traits<R> is a specialization of iterator_traits)\n return indirectly_readable_traits<R>::value_type;\n else\n return iterator_traits<R>::value_type;\nRun Code Online (Sandbox Code Playgroud)\n但在cppreference中,实现必须是:
\n\n计算
value typeT 的 。如果std::iterator_traits<std::remove_cvref_t<T>>不是专门化的,则为std::iter_value_t<T> …
我正在尝试对笛卡尔积进行变换,最终使边缘变平。我收到的结果不是范围,这就是问题所在:
#include <ranges>
#include <vector>
#include <iostream>
#include <tuple>
#include <iterator>
int main() {
auto indicies = std::views::iota(0, 3);
auto coords = std::views::cartesian_product(indicies, indicies);
auto edges = coords | std::views::transform([](const auto& coord) {
using Coord = std::pair<std::size_t, std::size_t>;
using Edge = std::pair<Coord, Coord>;
std::vector<Edge> ret;
const auto& [x, y] = coord;
auto curr = Coord{x, y};
if (x + 1 < 3) {
auto dest = Coord{x + 1, y};
ret.emplace_back(curr, dest);
}
if (y + 1 < 3) { …Run Code Online (Sandbox Code Playgroud) 我喜欢在个人项目中使用最新的 C++。我想知道如何在最新的 C++ 中延迟 lambda(或使用作用域保护)
下面是一个快速实现。我最喜欢款式3。标准有类似下面的内容吗?我对此有任何非常明显的错误吗?我只花了几分钟写这篇文章
#include<cstdio>
#include<memory>
using namespace std;
template<class T>
class DeferLambda {
T l;
public:
DeferLambda(T l) : l(l) { }
~DeferLambda() { l(); }
};
auto make_defer(auto p) -> DeferLambda<decltype(p)> { return {p}; }
#define NAME2(A, B) A ## B
#define NAME(A, B) NAME2(A, B)
#define L(X) auto NAME(defer, __LINE__) = make_defer(X);
int main(int argc, char *argv[])
{
DeferLambda a([]{puts("Style 1");});
auto b = make_defer([]{puts("Style 2");});
L([]{puts("Style 3");});
puts("test");
}
Run Code Online (Sandbox Code Playgroud) 使用 C++23 视图::join_with 时可以使用多字符串作为分隔符吗?
gcc 14 返回错误消息。
#include <format>
#include <stdio.h>
#include <string>
#include <algorithm>
#include <ranges>
using namespace std;
int main(){
const string a[2]={"6", "7"};
const string buffer1 = a | views::join_with('\n') | ranges::to<string>(); //OK
printf("%s", buffer1.c_str());
const string buffer2 = a | views::join_with(",\n") | ranges::to<string>(); //KO
printf("%s", buffer2.c_str());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
prog.cc: In function 'int main()':
prog.cc:13:30: error: no match for 'operator|' (operand types are 'const std::string [2]' {aka 'const std::__cxx11::basic_string<char> [2]'} and 'std::ranges::views::__adaptor::_Partial<std::ranges::views::_JoinWith, const char*>')
13 | …Run Code Online (Sandbox Code Playgroud) 我需要从另一个类的 std::vector 构造一个 std::discrete_distribution 。然而,我得到的一切都是错误的
无法创建大于 max_size() 的 std::vector
我试图将代码简化为基础(也错过了所有包含的内容,因为其他一切都工作正常)。另外,在花费了很短的时间尝试调试之后,我使用了一些控制台打印来找到我认为导致此问题的问题。
A类.h
class ClassA.h {
private:
std::vector<double> weights;
public:
ClassA();
[[nodiscard]] std::vector<double> getWeights();
Run Code Online (Sandbox Code Playgroud)
A类.cpp
ClassA::ClassA() {
for (int i = 0; i < 50; i++) {
initialPheromone.emplace_back(1);
std::cout << " " << initialPheromone[i]; // TESTING
}
}
std::vector<double> ClassA::getWeights() {
return weights;
}
Run Code Online (Sandbox Code Playgroud)
B.h类
class ClassB {
private:
ClassA *variable;
void someFun();
Run Code Online (Sandbox Code Playgroud)
B类.cpp
ClassB::ClassB(ClassA *classA1){
variable = classA1;
}
void someFun() {
// TESTING
for (auto thing = variable->getWeights().begin();
thing != …Run Code Online (Sandbox Code Playgroud) 左关联性已由 C++ 核心语义保证:
R|V1|V2被解析为(R|V1)|V2). 如果编码员想要显式地将操作顺序更改为 ,该怎么办R|(V1|V2)?这在 C++23 下可能吗?这个问题背后的原因是它简化了自定义范围适配器的定义:
auto myV=V1|V2|V3;
for(auto& x:R1|myV)/*..*/;
for(auto& x:R2|myV)/*..*/;
Run Code Online (Sandbox Code Playgroud)
所有这些需求似乎都是对 ; 的适当约束过载std::views::operator|;这种超载是否存在?如果没有的话会添加吗?如果不是,其背后的理由是什么?
与 cv 限定不同,ref 限定不会更改指针的属性
this:在右值 ref 限定函数内,*this仍然是左值表达式。
那么,我们如何获得 的 ref-qualifier *this,或者如何转发*this,以避免重复使用不同的 ref-qualifier 实现相同的成员函数呢?
以下get函数将使用可变参数返回一个字符串数组。它还通过简单地转换整数来处理提供整数的情况。我提供了这个转换函数的两个版本,一个是 lambda,另一个是自由函数。
请有人解释一下为什么使用 lambda 无法编译,而看似相同的自由函数却可以正常工作。需要进行哪些更改才能使 lambda 案例发挥作用?
#include <iostream>
#include <string>
#include <vector>
#include <ranges>
#include <format>
#include <array>
#include <type_traits>
// free function converter
template <typename KEY>
auto convert_to_string_view(const KEY& key)
{
if constexpr (std::is_integral_v<KEY> && !std::is_same_v<KEY, bool>)
return std::to_string(key);
else
return key;
}
// make an array out of varargs
template <typename T, typename... Args>
inline std::array<std::common_type_t<Args...>, sizeof...(Args)>
make_array_from_vars(const Args&... args)
requires (std::is_constructible_v<std::common_type_t<Args...>, Args> && ...)
{
return { args... };
}
template<typename... KEYS>
auto get(KEYS... …Run Code Online (Sandbox Code Playgroud) 我想知道标准委员会是否已经修复了臭名昭著的Hello, world! 漏洞。我主要谈论新<print>库(尚未在任何编译器中使用)。
{fmt}库(它启发了标准库)尚未修复此问题。显然,它在输出时不会抛出任何异常/dev/full(从 v9.1.0 开始)。因此,使用 CI/O 函数(例如std::fflush错误处理)仍然是一件事。
下面的程序注意到错误并返回失败代码(因此没有错误):
#include <exception>
#include <cstdio>
#include <cstdlib>
#include <fmt/core.h>
int main()
{
fmt::println( stdout, "Hello, world!" );
if ( std::fflush( stdout ) != 0 || std::ferror( stdout ) != 0 ) [[unlikely]]
{
return EXIT_FAILURE;
}
}
Run Code Online (Sandbox Code Playgroud)
但这在 C++23 中可能吗?
#include <print>
#include <exception>
#include <cstdio>
#include <cstdlib>
int main()
{
try
{
std::println( stdout, "Hello, world!" );
}
catch ( const std::exception& …Run Code Online (Sandbox Code Playgroud)