小编Moo*_*ooh的帖子

Visual Studio 2013中的C#方法重载解析问题

在Rx.NET库中提供这三种方法

public static IObservable<TResult> Create<TResult>(Func<IObserver<TResult>, CancellationToken, Task> subscribeAsync) {...}
public static IObservable<TResult> Create<TResult>(Func<IObserver<TResult>, CancellationToken, Task<IDisposable>> subscribeAsync) {...}
public static IObservable<TResult> Create<TResult>(Func<IObserver<TResult>, CancellationToken, Task<Action>> subscribeAsync) {...}
Run Code Online (Sandbox Code Playgroud)

我在MSVS 2013中编写以下示例代码:

var sequence =
  Observable.Create<int>( async ( observer, token ) =>
                          {
                            while ( true )
                            {
                              token.ThrowIfCancellationRequested();
                              await Task.Delay( 100, token );
                              observer.OnNext( 0 );
                            }
                          } );
Run Code Online (Sandbox Code Playgroud)

由于模糊的重载,这不会编译.编译器的确切输出是:

Error    1    The call is ambiguous between the following methods or properties: 
'System.Reactive.Linq.Observable.Create<int>(System.Func<System.IObserver<int>,System.Threading.CancellationToken,System.Threading.Tasks.Task<System.Action>>)' 
and 
'System.Reactive.Linq.Observable.Create<int>(System.Func<System.IObserver<int>,System.Threading.CancellationToken,System.Threading.Tasks.Task>)'
Run Code Online (Sandbox Code Playgroud)

但是只要我while( true )while( false …

c# rx.net

31
推荐指数
2
解决办法
1284
查看次数

建议适用于键范围查找的数据结构

我正在寻找类似于SCG.Dictionary的数据结构,但将数字范围作为键.

需要大部分性能的主要操作是查找与指定范围重叠的键.

例如,假设以下地图

[ 5, 15] -> King
[35, 50] -> Bear
[25, 40] -> Doll
Run Code Online (Sandbox Code Playgroud)

当[10,30]传递给搜索算法时,它必须回复以下条目:

[ 5, 15] -> King
[25, 40] -> Doll
Run Code Online (Sandbox Code Playgroud)

理想情况下,搜索方法应该返回IEnumerable而不是将结果复制到中间容器中.与SortedSet.GetViewBetween类似

使用模式将是符合的

var lookup = new RangeDictionary<int>();
lookup.Add( 5, 15, 'King' );
lookup.Add( 35, 50, 'Bear' );
lookup.Add( 25, 40, 'Doll' );

var results = lookup.FindIntersection( 10, 30 );
foreach( var pair in results )
  Console.WriteLine( "[{0}, {1}] -> {2}", pair.Key.From, pair.Key.To, pair.Value );
Run Code Online (Sandbox Code Playgroud)

有没有现成的解决方案?

c#

9
推荐指数
1
解决办法
387
查看次数

如何在 Rust 中调用泛型特征方法

拥有这些相当人为的类型定义

trait Generic<T> {
    fn some(&self) -> T;
}

impl<T> Generic<T> for i32
where
    T: Default,
{
    fn some(&self) -> T {
        T::default()
    }
}
Run Code Online (Sandbox Code Playgroud)

我想调用some显式指定类型 T 的方法。下面的代码显然不起作用,因为该方法本身不是通用的。

fn main() {
    let int: i32 = 45;
    println!( "some: {}", int.some<bool>() );
}
Run Code Online (Sandbox Code Playgroud)

正确的通话方式是什么some

rust

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

C++ 中的空尖括号

在探索 RxCpp 库时,我遇到了以下我无法解释的示例。

    auto ints = rxcpp::observable<>::create(
        [](rxcpp::subscriber<int> s){
            s.on_next(1);
            s.on_next(2);
            s.on_completed();
    });
Run Code Online (Sandbox Code Playgroud)

库中有两个observable类的声明:

template<class T, class SourceOperator>
class observable
    : public observable_base<T>
{
// ...
};

template<>
class observable<void, void>
{
// ...
};

Run Code Online (Sandbox Code Playgroud)

我无法理解的是编译器如何设法接受rxcpp::observable<>.一块。有可能是许多明确的专业化observable针对不同类型,比其他void,void

问题是编译器如何解释这段代码中的空尖括号: rxcpp::observable<>.

我在observable类中没有看到默认模板参数,也没有可以解释这一点的可变参数模板参数。

然后我认为它与显式模板专业化有某种关系,并试图在一个孤立的程序中重现它,例如像这样

namespace isolated {
  template<class T>
  class Test {
  public:
    static void say() {
      cout << "I am generic" << endl;
    }
  };

  template<>
  class Test<int> {
  public:
    static void say() …
Run Code Online (Sandbox Code Playgroud)

c++ rxcpp

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

标签 统计

c# ×2

c++ ×1

rust ×1

rx.net ×1

rxcpp ×1