Tag Info

Hot answers tagged

46

Besides the void * pointer which is covered in Robert's answer, a technique like this was used (Disclaimer: 20 year old memory): #define WANTIMP #define TYPE int #include "collection.h" #undef TYPE #define TYPE string #include "collection.h" #undef TYPE int main() { Collection_int lstInt; Collection_string lstString; } Where I have forgotten ...


43

The traditional way to implement generics without having generics (the reason templates were created) is to use a void pointer. typedef struct Item{ void* data; } Item; typedef struct Node{ Item Item; struct Node* next; struct Node* previous; } Node; In this example code, a binary tree or doubly-linked list can be represented. ...


37

Probably the biggest difference is that C macros are expanded in the preprocessing phase, before any other compiling is done, while C++ templates are part of compilation. This means that C++ templates are type-aware and scoped, among other things, and are not simple textual substitution. They can compile to real functions, and therefore avoid most of the ...


21

This is a very bad situation, you need to refactor this ASAP - this is technical debt in it's worst - you don't even know how important the code really is - only speculate that it's important. As to solutions ASAP: Something that can be done is adding a custom compilation step. If you use Maven that is actually fairly simple to do, other automated build ...


17

Basically, my question is, did I miss something? I believe you did, but it has less to do with programming languages and more to do with the human tendency to denigrate the unfamiliar. We do that. It's natural. Rising above it takes a willingness to endure the cognitive dissonance that comes with the comparisons to the familiar when learning ...


15

Is the maintenance hit real, or does it just bother you? If it only bothers you, leave it alone. Is the performance issue real, or did the previous developer only think it was? Performance problems, more often than not, are not where they are thought to be, even when a profiler is used. (I use this technique to reliably find them.) So there is a ...


15

As other answers pointed out, you can use void* for generic data structures. For other kinds of parametric polymorphism, preprocessor macros were used if something got repeated a lot (like dozens of times). To be honest, though, most of the time for moderate repetition, people just copied and pasted, then changed the types, because there are a lot of ...


13

Template error messages may be notorious, but are by no means always long and unreadable. In this case, the entire error message (from gcc) is: test.cpp: In function ‘void dosomething(T&) [with T = X]’: test.cpp:11: instantiated from here test.cpp:6: error: no match for ‘operator+=’ in ‘x += 5’ As in your Python example, you get a "stack trace" of ...


13

About a decade ago after a conference day I happened to find myself in a pub with Erwin Unruh and a few others and I asked him about the program VJovic mentioned. We have had a couple of beers by then, it was an unofficial meeting, and it's lived in my head for a decade, so take this with a grain of salt, but according to my memories: He said that they had ...


12

This is a specialized form of the more general question "how do you convince your superiors to start making use of a newer and better technology / way of making things?" Unfortunately, I do not think you can, and I am not sure you should. It is by definition their choice, since they are superior and they are paying you to do things in a certain way that ...


12

There are still people in the world who dont use jave generics in "ordinary coding." I can believe it with C++ templates, but generics? They aren't even hard to learn/use. Seriously the best features of Java and C++ are respectively generics and templates. The best way to convince people of things is to make a compelling argument, be non threatening, ...


10

In 1994 during a meeting of the c++ standardization commitee, E. Unruh discovered that templates can be used o compute something at compile time. He wrote a program that prints prime numbers. The intriguing part of this exercise was that the production of the prime numbers was performed by the compiler during the compilation process and not at ...


10

Actually, it seems you are just overcomplicating things, which might obfuscate your code. This sId and this sId<true> are just two different functions. And the standard way of implementing two different functions (with the same signature) is simply to use two different function names, without the template hoodoo voodoo, like sIdWithoutEvent and ...


9

First, the "STL" is not an official term, it is the name of the library proposed for inclusing in the C++ Standard Library when there was no containers. It provides essentially container and algorithms templates. Now, these container and algorithms templates are...templates so they produce types as needed. They don't rely on inheritance from the point of ...


8

The simple answer is, because Python was designed to work that way, whereas a lot of the stuff associated with templates came about by accident. It was never intended to become a Turing-complete system unto itself, for example. And if you can't deliberately plan and reason about what happens when your system works, why should anyone expect careful, ...


8

The one overriding principle: getting humans to write XML is evil.


8

The Standard of C++ is a normative document, which sets rules that will remain (mostly unaffected) in the future documents. Therefore the committee has taken a very cautious approach with regard to its updates. The additions to the standard library were somewhat easy. A number of libraries had been in Boost for a long time: it had been proved they worked. ...


8

JSON should just contain the data and no markup. In the long run this approach is more extensible because there is potential for using the JSON data in other parts of your site. If you include markup then using the same data to populate another template becomes much harder.


8

Books: Modern C++ design C++ Template Metaprogramming Being familiar with recursion and functional programming is a huge plus, as that's what a lot of tmp involves. It's turing complete and so essentially anything is possible, although usually it boils down to applying pure functions to constants or generating types from other types. Warning: tmp is an ...


8

Here's a link to a LinkedIN Article where they detail their comparison and present their results as well as why did they select what they did (dust.js). This should give you some fair idea about the comparisons and what people are most likely to look for in templating solutions on client side and what are their strengths/weaknesses in those areas.


8

Really make sure than under real, production, conditions (realistic memory consumption, that triggers Garbage Collection realistically, etc), all those separate methods really make a performance difference (as opposed to having only one method). This will take a few days out of your time, but may save you weeks by simplifying the code you work with from now ...


8

I remember when gcc shipped with genclass - a program which took as input a set of parameter types (e.g. key and value for a Map) and a special syntax file which described a parameterized type (say, a Map or a Vector) and generated a valid C++ implementations with the param types filled in. So if you needed Map<int, string> and Map<string, ...


7

I would broadcast the merits of generics to the code reviewer and other colleagues ahead of any review: 1) By allowing you to specify types that are acted on by a generic class or method, the generics feature shifts the burden of type safety from you to the compiler. There is no need to write code to test for the correct data type because it is enforced at ...


7

Actually, the STL introduced the Generic Programming paradigm to the mainstream. When I was taught about the Object-oriented Programming paradigm (by some professor) 15 years ago, I was taught the three crucial pillars are encapsulation, inheritance, and polymorphism (today the latter should probably be qualified as runtime polymorphism), while Wikipedia ...


7

You don't link to any of the 'weird information' you found, but there is no language construct in Perl called 'templates'. Generally, I expect the subject of any article on Perl and templating to be about text output; a template to generate HTML, or an email, for example. The Text::Template and HTML::Template modules being canonical examples.


7

[To the OP: I'm not trying to pick on you personally, but raise your and others' awareness of thinking about the logic of the question(s) asked on SE and elsewhere. Please don't take this personally!] The title of the question is good, but you are severely limiting the scope of your answers by including '... situations where they needed compile-time code ...


6

A few of the obvious reasons include: History. When gcc, MSVC, etc., were new, they couldn't afford to use lots of extra space to store data to produce better error messages. Memory was scarce enough that they just couldn't. For years, consumers ignored error message quality, so vendors mostly did too. With some code, the compiler can re-synchronize and ...


6

Learn Haskell or some other pure functional language (Lisp, Scheme, OCaml to name a few, you can find more on Wikipedia). Considering Haskell, you can find more information about it's metaprogramming facilities here. Template programming follow much the same rules actually.


6

Unless you are the architect/tech lead it will be difficult to mandate that you should use generics/templates. You really should propose the generic/template solution long before the code review, preferrably before you start implementing it. What you can do, is to demonstrate why you should use a generic in some case. If you can demonstrate the benefits, ...


6

As a learner of C++ Template Metaprogramming a few months ago, I would still recommend to read this book. It does not include C++11, but it lays out the syntax and the C++ specifications on how a compiler should process template code. The book covers a lot of less known corners which will become crucial if you need to use C++ templates substantially, ...



Only top voted, non community-wiki answers of a minimum length are eligible