generic-programming

Tag dispatch versus static methods on partially specialised classes

一个人想着一个人 提交于 2019-11-28 02:53:59
Suppose I want to write a generic function void f<T>() , which does one thing if T is a POD type and another thing if T is non-POD (or any other arbitrary predicate). One way to achieve this would be to use a tag-dispatch pattern like the standard library does with iterator categories: template <bool> struct podness {}; typedef podness<true> pod_tag; typedef podness<false> non_pod_tag; template <typename T> void f2(T, pod_tag) { /* POD */ } template <typename T> void f2(T, non_pod_tag) { /* non-POD */ } template <typename T> void f(T x) { // Dispatch to f2 based on tag. f2(x, podness<std::is

How much existing C++ code would break if void was actually defined as `struct void {};`

心不动则不痛 提交于 2019-11-28 02:30:52
问题 void is a bizarre wart in the C++ type system. It's an incomplete type that cannot be completed, and it has all sort of magic rules about the restricted ways it can be employed: A type cv void is an incomplete type that cannot be completed; such a type has an empty set of values. It is used as the return type for functions that do not return a value. Any expression can be explicitly converted to type cv void ([expr.cast]). An expression of type cv void shall be used only as an expression

Avoiding Java Type Erasure

心不动则不痛 提交于 2019-11-27 22:17:30
问题 Is there a way one could avoid type erasure and get access to a type parameter? public class Foo<T extends Enum<?> & Bar> { public Foo() { // access the template class here? // i.e. : baz(T.class); // obviously doesn't work } private void baz(Class<T> qux) { // do stuff like T[] constants = qux.getEnumConstants(); ... } } I need to know about T , and do things with it. Is it possible, and if so, how can it be done without passing in the class in the constructor or anywhere besides the

templates problem ('typename' as not template function parameter)

穿精又带淫゛_ 提交于 2019-11-27 16:55:50
问题 Actually I've a problem with compiling some library with intel compiler. This same library has been compiled properly with g++. Problem is caused by templates. What I'd like to understand is the declaration of **typename** as not template function parameter and variable declaration inside function body example: void func(typename sometype){.. ... typename some_other_type; .. } Compilation this kind of code produce following errors (intel),(gcc doesn't claim): I've got following errors ../../.

What are the disadvantages of using templates?

佐手、 提交于 2019-11-27 16:19:49
问题 Some of the disadvantages would be its syntax is complex compiler generates extra code 回答1: They are hard to validate. Template code which doesn't get used tends to be seldom compiled at all. Therefore good coverage of test cases is a must. But testing is time-consuming, and then it may turn out the code never needed to be robust in the first place. 回答2: Hmm, how about... 3: They can be slow to compile 4: They force things to be calculated at compile time rather than run time (this can also

Can someone explain what does <? super T> mean and when should it be used and how this construction should cooperate with <T> and <? extends T>?

孤街醉人 提交于 2019-11-27 14:47:52
问题 I'm using generics rather long time but I've never used construction like List<? super T> . What does it mean? How to use it? How does it look after erasure? I also wonder: is it something standard in generic programming (template programming?) or it's just a java 'invention'? Does c#, for example, allow similar constructions? 回答1: This construct is used when you want to consume items from a collection into another collection. E.g. you have a generic Stack and you want to add a popAll method

Template function as a template argument

佐手、 提交于 2019-11-27 11:34:58
I've just got confused how to implement something in a generic way in C++. It's a bit convoluted, so let me explain step by step. Consider such code: void a(int) { // do something } void b(int) { // something else } void function1() { a(123); a(456); } void function2() { b(123); b(456); } void test() { function1(); function2(); } It's easily noticable that function1 and function2 do the same, with the only different part being the internal function. Therefore, I want to make function generic to avoid code redundancy. I can do it using function pointers or templates. Let me choose the latter

'Multipurpose' linked list implementation in pure C

限于喜欢 提交于 2019-11-27 09:32:56
问题 This is not exactly a technical question, since I know C kind of enough to do the things I need to (I mean, in terms of not 'letting the language get in your way'), so this question is basically a 'what direction to take' question. Situation is: I am currently taking an advanced algorithms course, and for the sake of 'growing up as programmers', I am required to use pure C to implement the practical assignments (it works well: pretty much any small mistake you make actually forces you to

Generic pair class

送分小仙女□ 提交于 2019-11-27 08:56:17
Just attempting this question I found in a past exam paper so that I can prepare for an upcoming Java examination. Provide a generic class Pair for representing pairs of things. The class should provide a constructor, a method for getting the first member of the pair, a method for getting the second member of the pair, a method for setting the first member of the pair, a method for setting the second member of the pair. The class should be parameterised over two types one for the first member and one for the second member of the pair. Is this a correct implementation for this question ? public

Trait for numeric functionality in Rust

[亡魂溺海] 提交于 2019-11-27 05:35:48
Is there any trait that specifies some numeric functionality? I'd like to use it for bounding a generic type, like this hypothetical HasSQRT : fn some_generic_function<T>(input: &T) where T: HasSQRT { // ... input.sqrt() // ... } evilone You can use num or num-traits crates and bound your generic function type with num::Float , num::Integer or whatever relevant trait: extern crate num; use num::Float; fn main() { let f1: f32 = 2.0; let f2: f64 = 3.0; let i1: i32 = 3; println!("{:?}", sqrt(f1)); println!("{:?}", sqrt(f2)); println!("{:?}", sqrt(i1)); // error } fn sqrt<T: Float>(input: T) -> T