generic-programming

Templates inferring type T from return type

霸气de小男生 提交于 2021-02-10 05:44:07
问题 I have a template as follows: template <class T> vector<T> read_vector(int day) { vector<T> the_vector; {...} return the_vector; } I would like to be able to do something like vector<int> ints = read_vector(3); vector<double> doubles = read_vector(4); Is it possible for C++ templates to infer the return type from when they're called, or should I just pass a dummy argument to the template with the type I want to the vector to have? The latter works but is messier. 回答1: #include <vector> struct

Java generic class and wildcards

跟風遠走 提交于 2021-02-07 19:14:46
问题 I've got a problem with generic classes in java. I've got this class: public abstract class MyMotherClass<C extends AbstractItem> { private C item; public void setItem(C item) { this.item = item; } public C getItem() { return item; } } An implementation of this class can be: public class MyChildClass extends MyMotherClass<ConcreteItem> { } ConcreteItem is just a simple class that extends AbstractItem (which is abstract). so MyChildClass have a ConcreteItem and I can use: MyChildClass child =

Java generic class and wildcards

﹥>﹥吖頭↗ 提交于 2021-02-07 19:11:57
问题 I've got a problem with generic classes in java. I've got this class: public abstract class MyMotherClass<C extends AbstractItem> { private C item; public void setItem(C item) { this.item = item; } public C getItem() { return item; } } An implementation of this class can be: public class MyChildClass extends MyMotherClass<ConcreteItem> { } ConcreteItem is just a simple class that extends AbstractItem (which is abstract). so MyChildClass have a ConcreteItem and I can use: MyChildClass child =

Java generic class and wildcards

喜欢而已 提交于 2021-02-07 19:04:47
问题 I've got a problem with generic classes in java. I've got this class: public abstract class MyMotherClass<C extends AbstractItem> { private C item; public void setItem(C item) { this.item = item; } public C getItem() { return item; } } An implementation of this class can be: public class MyChildClass extends MyMotherClass<ConcreteItem> { } ConcreteItem is just a simple class that extends AbstractItem (which is abstract). so MyChildClass have a ConcreteItem and I can use: MyChildClass child =

Amazon API Gateway Import From Swagger Error - Not taking Generics

随声附和 提交于 2021-02-07 14:22:44
问题 I'm trying to create new APIGateway via import from Swagger, but having validation errors: The particular class causing the issue is our PaginationModel class. Code model definition: public class PaginationModel<T> { public IEnumerable<T> items { get; set; } public int offset { get; set; } public int totalCount { get; set; } } Swagger file section representing Generic PaginationModel for a particular type: *"PaginationModel[DepartmentUIModel]":{"type":"object","properties":{"items": {"type":

Amazon API Gateway Import From Swagger Error - Not taking Generics

人盡茶涼 提交于 2021-02-07 14:21:35
问题 I'm trying to create new APIGateway via import from Swagger, but having validation errors: The particular class causing the issue is our PaginationModel class. Code model definition: public class PaginationModel<T> { public IEnumerable<T> items { get; set; } public int offset { get; set; } public int totalCount { get; set; } } Swagger file section representing Generic PaginationModel for a particular type: *"PaginationModel[DepartmentUIModel]":{"type":"object","properties":{"items": {"type":

Does the current SYB permit extension of generic functions with new types?

不想你离开。 提交于 2021-01-28 12:35:33
问题 The first two Scrap Your Boilerplate papers describe a way of writing generic functions that work for general types, but have special cases for specific types. For instance, fromJSON from the aeson package, defines a generic function for converting from JSON, but provides special cases for types like list or Int : parseJSON :: (Data a) => Value -> Parser a parseJSON j = parseJSON_generic j `ext1R` list `ext1R` vector `ext2R'` mapAny `ext2R'` hashMapAny -- Use the standard encoding for all

Can I use a scoped enum for C++ tag dispatch with templates?

拈花ヽ惹草 提交于 2021-01-27 21:29:46
问题 Template newbie here. I'm experimenting with the following code: #include <type_traits> enum class Thread { MAIN, HELPER }; template<typename T> int f() { static_assert(std::is_same_v<T, Thread::MAIN>); return 3; } int main() { f<Thread::MAIN>(); } In words, I want to raise a compile-time assert if the function is not called from the main thread. Apparently the compiler doesn't accept an enumerator as template argument, yelling invalid explicitly-specified argument for template parameter 'T'

“template” VHDL entities

六眼飞鱼酱① 提交于 2021-01-27 13:48:42
问题 This has me bugging for quite some time, but is it possible to describe entities in VHDL similar to how templates work in C++ (or to lesser extend generics?). Simply leaving the actual port types to be only decided during synthesize/compilation? An example would be a multiplexer, say I have a 4 input multiplexer, now I have several bus sizes I use this multiplexer for, -4,6,7,8-. Currently I wrote a different multiplexer for each different bus size; however the output is simply one of the

Are there any ways to recursively flatten tuples?

北战南征 提交于 2021-01-20 05:58:14
问题 In Rust, is there any way to use trait s and impl s to (recursively) flatten tuples? If it helps, something that works with N nested pairs is a good start trait FlattenTuple { fn into_flattened(self) -> /* ??? */ } // such that assert_eq!((1, (2, 3)).into_flattened(), (1, 2, 3)) It would be even better if it could be extended work with any kind of nested tuple such that: assert_eq!(((1, 2), 2, (3, (4, 5))).into_flattened(), (1, 2, 2, 3, 4, 5)) 回答1: Maybe for certain small definitions of