我正在尝试将视图创建为转换类型的向量。从文档中我读到以下内容应该有效,但编译器输出非常混乱。我缺少什么?
#include <ranges>
#include <vector>
int main() {
std::vector<int> v {1, 2, 3};
auto view = v | std::views::transform([](int i){ return std::to_string(i); });
}
Run Code Online (Sandbox Code Playgroud)
编译器输出:
In file included from <source>:1:
In file included from /opt/compiler-explorer/gcc-11.2.0/lib/gcc/x86_64-linux-gnu/11.2.0/../../../../include/c++/11.2.0/ranges:43:
In file included from /opt/compiler-explorer/gcc-11.2.0/lib/gcc/x86_64-linux-gnu/11.2.0/../../../../include/c++/11.2.0/iterator:61:
In file included from /opt/compiler-explorer/gcc-11.2.0/lib/gcc/x86_64-linux-gnu/11.2.0/../../../../include/c++/11.2.0/bits/stl_iterator_base_types.h:71:
/opt/compiler-explorer/gcc-11.2.0/lib/gcc/x86_64-linux-gnu/11.2.0/../../../../include/c++/11.2.0/bits/iterator_concepts.h:980:13: error: no matching function for call to '__begin'
= decltype(ranges::__cust_access::__begin(std::declval<_Tp&>()));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/lib/gcc/x86_64-linux-gnu/11.2.0/../../../../include/c++/11.2.0/bits/ranges_base.h:586:5: note: in instantiation of template type alias '__range_iter_t' requested here
using iterator_t = std::__detail::__range_iter_t<_Tp>;
^
/opt/compiler-explorer/gcc-11.2.0/lib/gcc/x86_64-linux-gnu/11.2.0/../../../../include/c++/11.2.0/bits/ranges_util.h:98:43: note: in instantiation of template type …Run Code Online (Sandbox Code Playgroud) 我正在调试大量模板化代码,并寻找一种方法来隐藏回溯中和打印变量时的类型信息。如果您可以仅隐藏模板参数,那就更好了,因为它们使回溯很难阅读。
感谢您的支持。
目前是否可以编写函数,它接受const合格对象的引用,但没有r值?
// Takes no r-values
void foo(std::string& v) {
...
}
...
const string cstr("constant string");
foo("string"); // this does not compile, as wanted
foo(cstr); // this does not compile, as expected. But i would want it to
...
Run Code Online (Sandbox Code Playgroud)
// Takes r-values, which is highly undesired
void foo2(const std::string& v) {
...
}
...
const string cstr("constant string");
foo("string"); // this does compile, but i want it to fail.
foo(cstr); // this does compile
...
Run Code Online (Sandbox Code Playgroud)
问题的背景与稍后的对象副本有关(在foo完成之后).基本上,引用被推送到队列并稍后处理.我知道,语义混乱,需要一个shared_ptr或类似的东西.但我与外部约束有关. …