English 中文(简体)
为什么C++范围适应性封闭管道运营商没有工作
原标题:Why is C++ range adaptor closure pipeline operator not working

我正在制作一个<代码>版本:奇怪:to作为学习经验。 我的版本名称为convert。 既然是<条码>至。 <代码>#if中的代码将不汇编,但我不理解为什么。

一位猜测是,尽管有<条码>truct的继承,但管道运营商仍没有工作,因为有一些Im失踪。

另一种是<代码>convert<vector>的表述是不合逻辑的,因为其他观点是t模板。

我不想制定生产水平的法典,只学习这些机制。

可在Compiler Exploration上查阅。

#include <fmt/core.h>
#include <fmt/format.h>
#include <fmt/ranges.h>

#include <ranges>
#include <string_view>
#include <vector>

namespace rng = std::ranges;
namespace vws = std::views;
namespace detail {
//------------------------------------------------
template <typename ConT>
struct Convert : rng::range_adaptor_closure<Convert<ConT>> {
    template <rng::range Rng>

    constexpr auto operator()(Rng& src) -> ConT {
        return ConT{rng::begin(src), rng::end(src)};
    }
};

template <typename ConT>  //
inline detail::Convert<ConT> convert;
}  // namespace detail
//--------------------------------------------------
// handles container with no <type>: to<vector>
template <template <typename> typename Con, rng::range Rng,
          typename T = rng::range_value_t<Rng>>
auto convert(Rng&& src) -> Con<T> {
    return detail::convert<Con<T>>(src);
}
//--------------------------------------------------
int main() {
    constexpr std::string_view data = "01234567";

    auto vec1 = convert<std::vector>(data);
    auto vec2 = convert<std::vector>(data | vws::take(20));
    fmt::println("{}
{}
", vec1, vec2);

#if 0
    auto vec3 = data | vws::take(1) | convert<std::vector>();
    fmt::println("{}
", vec3);
#endif
}

<代码>#if改为#if 1的错误为:

<source>:41:59: error: no matching function for call to  convert<template<class _Tp, class _Alloc> class std::vector>() 
   41 |     auto vec3 = data | vws::take(1) | convert<std::vector>();
      |                                       ~~~~~~~~~~~~~~~~~~~~^~
<source>:29:6: note: candidate:  template<template<class> class Con, class Rng, class T>  requires  range<Rng> Con<T> convert(Rng&&) 
   29 | auto convert(Rng&& src) -> Con<T> {
      |      ^~~~~~~
<source>:29:6: note:   candidate expects 1 argument, 0 provided

最新情况:无,我不正确地指出这一点。 这是它应当如何工作。 见ranges:to on cppvis

功能范围:convert<std:vector>data):data hexachloro<stctor:vector,条件是vertrange Adaptationor,因为我已尝试执行。 我显然有这种错误,但这不是因为我应该说话。

更新:

@super provided code that works but it implements an operator|. Per the cpprefernce example for range_adaptor_closure that shouldn t be necessary. There is also a no-argument version of convert, which I also find in the GCC 14 / trunk code for ranges::to. Deleting the pipeline operator provides error messages from the ranges library that may help me determine why my code doesn t work.

问题回答

不会编纂,但我不理解为什么。

The expression convert<std:vector>> is a calls expression and it needs an debate due to the non-defaulted paraemter src. 但是,由于你没有就相应的参数(<代码>src)提出任何论点,你会发现上述错误。

<source>:41:59: error: no matching function for call to  convert<template<class _Tp, class _Alloc> class std::vector>() 
   41 |     auto vec3 = data | vws::take(1) | convert<std::vector>();
      |                                       ~~~~~~~~~~~~~~~~~~~~^~

请注意,您没有提供超负荷版本的<代码>convert,该文本没有参数,可以把左手作为操作。 ∗∗∗∗ 您可以提供这样的版本,即没有争辩和回报,如本demo所示,可以把左手带上。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签