generic-programming

std::insert_iterator and iterator invalidation

倖福魔咒の 提交于 2019-12-11 13:55:15
问题 I tried writing a generic, in place, intersperse function. The function should intersperse a given element into a sequence of elements. #include <vector> #include <list> #include <algorithm> #include <iostream> template<typename ForwardIterator, typename InserterFunc> void intersperse(ForwardIterator begin, ForwardIterator end, InserterFunc ins, // we cannot use rvalue references here, // maybe taking by value and letting users feed in std::ref would be smarter const ForwardIterator::value

Generically comparing objects in an inheritance hierarchy in c++

寵の児 提交于 2019-12-11 13:48:03
问题 I am planning to write a multimap like this std::multimap <key, base_ptr> mymap; And I would like to be able to store pointers of many derived classes (say Der1 , Der2 ) which derive from the base. Now when I am trying to insert an object into the map, I first do a lookup on the key and then I need to compare if the object is EQUIVALENT (does not have to be the same object hence not doing a pointer comparison) to the one at that location. So for this lets say I override the == operator or

How can I implement a function differently depending on if a generic type implements a trait or not?

◇◆丶佛笑我妖孽 提交于 2019-12-11 12:18:02
问题 I'd like to make the implementation of do_something conditional based on if the generic type T implements Debug or not. Is there any way to do something like this? struct A(i32); #[derive(Debug)] struct B(i32); struct Foo<T> { data: T, /* more fields */ } impl<T> Foo<T> { fn do_something(&self) { /* ... */ println!("Success!"); } fn do_something(&self) where T: Debug, { /* ... */ println!("Success on {:?}", self.data); } } fn main() { let foo = Foo { data: A(3), /* ... */ }; foo.do_something(

Select function name based on template parameter

可紊 提交于 2019-12-11 11:22:50
问题 Is there a way to automatically select between multiple non-template functions based on a template parameter? Example: class Aggregate { public: std::string asString(); uint32_t asInt(); private: // some conglomerate data }; template <typename T> T get(Aggregate& aggregate) { // possible map between types and functions? return bind(aggregate, typeConvert[T])(); ?? // or return aggregate.APPROPRIATE_TYPE_CONVERSION(); } The solution would be nice to throw a compiler error if there is no good

How to set Property value using Reflection?

僤鯓⒐⒋嵵緔 提交于 2019-12-11 11:01:49
问题 I try to build a Generic Method which take class and set value using reflection and return a class type. protected static T GetSecureModel<T>(T model) { T secureModel = default(T); foreach (var property in model.GetType().GetProperties()) { if (string.CompareOrdinal(property.PropertyType.FullName, "System.String") == 0) { property.SetValue(property.Name, property.GetValue(model, null).ToString(), null); } } return secureModel; } How to return a Class after set value ? 回答1: OK. I solve it.

How to describe the relationship between elements of tuple in array of tuples via types in TypeScript?

大兔子大兔子 提交于 2019-12-11 06:17:28
问题 I want to describe the relationship between elements of tuple in array of tuples via types in TypeScript. Is this possible? declare const str: string; declare const num: number; function acceptString(str: string) { } function acceptNumber(num: number) { } const arr /*: ??? */ = [ [str, acceptString], [num, acceptNumber], [str, acceptNumber], // should be error ]; for (const pair of arr) { const [arg, func] = pair; func(arg); // should be no error } TypeScript Playground link Real-world

What is the inverse notion of “refinement”

最后都变了- 提交于 2019-12-11 04:08:00
问题 In the world of generic programing the notion of refinement is very common. In particular given a concept C1 , then we say that a concept C2 refines C1 if it provides all the functionalities of C1 and possibly more. How do you call the inverse relation? So if C2 is a refinement of C1 then C1 is a what of C2 ? 回答1: Since the related transformation of requirements is called "lifting" I suggest the same for concepts. C1 is a lifting of C2 . However someone with native English should better help

Scala HList manipulation : is a flexible structure possible?

大憨熊 提交于 2019-12-11 01:05:15
问题 I wrote a min example of something I do not manage to do. Am I in the wrong way? object minNotWoringkExample { import shapeless._ def typed[T](t: => T) {} class Bar { type L <: HList } class Foo { type UPDATE[L <: HList] <: HList } class FooI extends Foo { type UPDATE[L <: HList] = FilterNot[L, FooI]#Out } def update[O <: Foo, B <: Bar] = new Bar { type L = O#UPDATE[B#L] } val myBar = new Bar { type L = FooI :: Foo :: HNil } val myUpdatedBar = update[FooI, Bar {type L = FooI :: Foo :: HNil}]

Invalid use of non-static member function In instantiation of member function of a class template?

别来无恙 提交于 2019-12-10 22:23:27
问题 I want a class template to start some number of threads to test some functions, which access some shared states. #include <vector> #include <thread> using namespace std; template<std::size_t M, std::size_t N> class A { public: void test(std::size_t n) { std::vector<std::thread> ts; for(int i = 0; i < N; ++i){ ts.push_back( std::thread( A::foo, this, i, n ) ); } for(auto& thread : ts){ thread.join(); } } private: void foo( std::size_t tid, std::size_t n ) { } }; int main() { A<10, 2> tester;

Ambiguous type variable

喜夏-厌秋 提交于 2019-12-10 20:45:44
问题 Related to my earlier question on traversing data structures, I'm having a problem making my code generic when I use it along with the uniplate package. I'm dealing with the data structures in the Language.Exts.Annotated.Syntax module, which are all generic with a type parameter l . This l is the same throughout the tree. The kind of code I'm writing is like this: doInt :: Child1 l -> Child1 l doInt (Child1 l n) = Child1 l (n + 1) doString :: Child2 l -> Child2 l doString (Child2 l (_:s)) =