generic-programming

Get the address of an object that has overloaded operator&

雨燕双飞 提交于 2019-12-24 10:58:47
问题 operator& for CComPtr is overloaded, which makes my generically written code crash. I am wondering if there is any way to force a variable into returning the address of it's object? 回答1: The solution is hideous: reinterpret_cast<CComPtr*>(&reinterpret_cast<char&>(ptr)) 回答2: If you can use boost, consider using: addressof, if you cannot, take a look at the implementation. 回答3: You could cast the CComPtr to a CComPtrBase , which, from what I can gather from the MSDN page, has no overloaded

When is type checking for type argument performed?

浪子不回头ぞ 提交于 2019-12-24 09:48:32
问题 Here is some C++ template code from Programming Language Pragmatics, by Scott template<typename T> class chooser { public: virtual bool operator()(const T& a, const T& b) = 0; }; template<typename T, typename C> class arbiter { T* best_so_far; C comp; public: arbiter() { best_so_far = nullptr; } void consider(T* t) { if (!best_so_far || comp(*t, *best_so_far)) best_so_far = t; } T* best() { return best_so_far; } }; class case_sensitive : chooser<string> { public: bool operator()(const string&

Inferred generic function typechecks as a return type but not an argument type

*爱你&永不变心* 提交于 2019-12-24 01:59:30
问题 I'm learning about SYB and rank n types, and came across a confusing case of what seems like the monomorphism restriction. I wrote a function to find the shallowest entry that matches a predicate. Instead of a reducing function, I wanted to accept a more predicate-like function using Alternative , and transform it into a generic function myself. I decided to omit the type annotation in the let block to see how the monomorphism reduction would affect the type in this implementation: shallowest

Caugth ClassCastException in my Java application

淺唱寂寞╮ 提交于 2019-12-23 19:40:35
问题 A queue implementaion using an array but i m getting an exception. I have an interface named as Queue with generic ArrayQueue as implemented class of Queue Interface ArrayQueueTest as my main class to test the code. public interface Queue<E> { public void enqueue(E e);//insert an element in a queue public E dequeue();/delete an element in queue and return that element public int size();//give the number of elements in an queue public E first();//give the first element of queue if any but not

Java: Integer obj can't cast to Comparable

喜你入骨 提交于 2019-12-23 17:07:37
问题 I'm having problems trying to pass an Integer object from a driver class as an argument for function of a SortedArray Generic class I created. From my driver class, I convert the user's int input into an Integer object to be cast onto Comparable of my SortedArray class. I continue to receive the error: "Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to Comparable". I took a look at some of my classmates' source codes only to find little difference in

Implementing a generic incrementable trait in Rust

不羁的心 提交于 2019-12-23 16:27:36
问题 I'm trying to understand how to implement a generic trait in Rust. While I've seen a number of examples, the examples are too tied to a specific use (e.g. genomic mutators) for me to be able to understand at this point in my Rust development. Instead, here's a simple example based on something fairly universal--incrementing: trait Incrementable { fn post_inc(&mut self) -> Self; fn post_inc_by(&mut self, n: usize) -> Self; } impl Incrementable for usize { fn post_inc(&mut self) -> Self { let

Composable C++ Function Decorators

南笙酒味 提交于 2019-12-23 14:56:56
问题 Python has a very useful feature of function decorators, which, moreover, allows composition. For example, if write a function foo , then you can state that you would like foo to be memoized, but also retried more than a single time in case of a cache miss in which foo also raises an exception, by: @lru_cache @retry def foo(...): Decorator composability allows developing functions like foo and individual function decorators independently, and then mixing them as needed. It would be nice if we

Composable C++ Function Decorators

自作多情 提交于 2019-12-23 14:56:46
问题 Python has a very useful feature of function decorators, which, moreover, allows composition. For example, if write a function foo , then you can state that you would like foo to be memoized, but also retried more than a single time in case of a cache miss in which foo also raises an exception, by: @lru_cache @retry def foo(...): Decorator composability allows developing functions like foo and individual function decorators independently, and then mixing them as needed. It would be nice if we

C++ Concepts: Can I define a concept that is itself a template?

岁酱吖の 提交于 2019-12-23 12:23:18
问题 Sorry if the question isn't too clear. I'm not sure the best way to phrase it (feel free to edit!). I think an example would be the most clear: I am attempting to define a Monad concept based off of the Haskell definition. The bind operator ( >>= ) requires that a Monad of type A can be bound to a function that takes an A and returns a Monad of type B . I can define A in terms of a value_type typedef but how do I define type B in my concept? template <typename M> concept bool Monad() { return

DataTemplate.DataType = Collection<Entity>?

我是研究僧i 提交于 2019-12-23 07:47:52
问题 Is there a way to create a data template that handles a list of items? I have Contact.Phones ( EntityCollection<Phone> ) and I want the data template to handle the list - add remove edit etc. Is there a way to set the DataType property of the DataTemplate to generic EntityCollection<Phone> ? 回答1: Wrap your generic list in a new class, which subclasses your generic list without adding anything. This makes it possible to bind to the list from XAML. This is described here: Can I specify a