generic-programming

Dynamic filtering, sorting and paging on generic list with Entity Framework

北战南征 提交于 2020-05-01 12:15:12
问题 I am using jQuery datatable (http://www.datatables.net) in my MVC4 app and as you may know this table allows server side processing. I am going to use the table in multiple views tied to multiple controllers so I'd like to implement a generic way to filer, sort and page data without the need to write a method for each controller. If I were to do that, they would all look the same but they would target a different entity from the database and be doing textual filtering and sorting on different

Is there a generic way to pass pointers of overloaded methods that requires less work (than my example)

二次信任 提交于 2020-03-23 08:05:37
问题 So I have a function where, using C++17, I'm able to apply any method from any object: #include <functional> template <typename Object, typename Method, typename ... Args> void ApplyMethod (Object && object, Method && method, Args && ... args) { std::invoke(method, object, args...); } What I ask: Is there a way to improve this to require less work for the caller of the function when the method is overloaded. Example use with overloaded methods: #include <iostream> class Foo { int bottles;

Iterate through template parameter

只谈情不闲聊 提交于 2020-03-05 02:07:31
问题 I have a function that receives a template parameter. template<class Container> void function(const Container& object) { //here i want to iterate through object and print them } int main() { function(std::vector<int>{1,3,6,7}); function(std::vector<std::vector<int>>{{1,2,3},{2,5,7}}); } Is it possible to do this in one function? Suppose the container argument will be integer. 回答1: One example: template<class T> void print(T const& object) { std::cout << object; } template<class... Args> void

What's the point of unnamed non-type template parameters?

蓝咒 提交于 2020-02-02 10:06:26
问题 According to the reference, the name of a non-type template parameter is optional, even when assigning a default value (see (1) and (2)). Therefore these template structs are valid: template <int> struct Foo {}; template <unsigned long = 42> struct Bar {}; I haven't seen a possibility of accessing the values of the non-type parameters. My question is: What's the point of unnamed/anonymous non-type template parameters? Why are the names optional? 回答1: What's the point of unnamed/anonymous non

Identifying Differences Efficiently

拜拜、爱过 提交于 2020-02-02 05:38:22
问题 Every day, we receive huge files from various vendors in different formats (CSV, XML, custom) which we need to upload into a database for further processing. The problem is that these vendors will send the full dump of their data and not just the updates. We have some applications where we need only send the updates (that is, the changed records only). What we do currently is to load the data into a staging table and then compare it against previous data. This is painfully slow as the data

compile time assertion in c++

Deadly 提交于 2020-01-24 20:41:06
问题 I am reading a compile time assert, after searching online I got some code which I didn't understand. template <bool> struct CompileAssert {}; #define COMPILE_ASSERT(expr, msg) \ typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] Used this COMPILE_ASSERT as below. COMPILE_ASSERT(!sizeof(T*), PassRefPtr_should_never_be_assigned_to) But I didnot got the Idea.Can someone help me In understanding the above piece of code. Second got confused on this piece of code typedef CompileAssert<

Get the result of Func<object> when object is a Task<Something>

谁说胖子不能爱 提交于 2020-01-24 10:11:46
问题 I am currently using this code to attempt to dynamically execute a saved Func<object> : public async Task<object> GetFuncResult(string funcName) { Func<object> func = _savedFuncs[funcName]; bool isAwaitable = func.Method.ReturnType.GetMethod(nameof(Task.GetAwaiter)) != null; if (!isAwaitable) return func(); else return await ((Func<Task<object>>)func)(); } If somebody stores a Func<Task<object>> or a Func<[anything]> this code works fine. But if somebody stores a Func<Task<string>> (or any

Java compiler not able to infer type on generic chain

≯℡__Kan透↙ 提交于 2020-01-24 07:57:05
问题 As mentioned below (to remark it as I might have explained myself badly): I want to understand the principle behind this issue so that I can apply that knowledge to the real problem. ISSUE START I am working in a system intended to be an abstract library to be used by many subsystems. The idea is to have an standard behaviour extensible by implementations. My problem is that java compiler is not able to inferr method parameter type, even though it makes no sense since the boundaries are well

How to avoid type checking in C# to load correct UI?

拜拜、爱过 提交于 2020-01-23 10:39:43
问题 I am building a simple quiz program with WinForms that has 2 modes: 1) Edit mode: Where the user can create its own questions 2) Quiz mode: Where the user needs to answer the questions Currently there are 2 type of questions: Open (question and free text box) and multiple choice (question and 4 possible answers). I have created an abstract class for a Question: public abstract class Question { public string QuestionString { get; private set; } public Question(string q) { QuestionString = q; }

What's the difference between a trait's generic type and a generic associated type?

独自空忆成欢 提交于 2020-01-23 05:45:08
问题 This question is asked before generic associated types are available in Rust, although they are proposed and developed. My understanding is that trait generics and associated types differ in the number of types which they can bind to a struct. Generics can bind any number of types: struct Struct; trait Generic<G> { fn generic(&self, generic: G); } impl<G> Generic<G> for Struct { fn generic(&self, _: G) {} } fn main() { Struct.generic(1); Struct.generic("a"); } Associated types bind exactly 1