Skip to main content
14 votes
Accepted

Way for generating C# classes from existing C# class

If that's all you have and it isn't practical / feasible for you to have a common single source of truth to generate your different artifacts, you may find the T4 template feature helpful. For ...
YSharp's user avatar
  • 888
13 votes
Accepted

C++ Preferred method of dealing with implementation for large templates

When writing a templated C++ class, you usually have three options: (1) Put declaration and definition in the header. // foo.h #pragma once template <typename T> struct Foo { void f() ...
pschill's user avatar
  • 1,980
12 votes

Why doesn't C++ support covariance in STL containers like C# or Java?

The reason is the underlying object and memory models. To simplify the reasoning: In java and C#, objects of a class are managed by reference. Containers do not store directly the object value but ...
Christophe's user avatar
  • 79.9k
10 votes
Accepted

How to generate java class files with framework boilerplate in a project?

I think the word you're looking for is a template. Most IDEs support some kind of template mechanism; specifically in the Java world, if you happen to use IntelliJ, it supports multi-file templates ...
casablanca's user avatar
  • 4,964
9 votes
Accepted

Create C++14 library where each class has 2 similar variants

One possible solution is to use templates and move the CPU / GPU specific stuff into a traits class: struct CPUBackendTraits { template <typename T> using vector_type = std::vector<T&...
pschill's user avatar
  • 1,980
8 votes

Was template meta programming really discovered "by accident"?

There is more to invention than giving a simple list of implications and with C++ Stroustrup aimed for generality: I'm not interested in a language that can only do what I can imagine This is from ...
manlio's user avatar
  • 4,236
7 votes
Accepted

C++ design pattern for template instantiation based on run time information?

A switch-case statement isn't that bad: It is easy to implement, easy to understand, and easy to maintain if the number of allowed values is limited (say less than 30). However, if the number of ...
pschill's user avatar
  • 1,980
7 votes
Accepted

C++ behavior as a template argument

It's absolutely fine and perfectly normal. Modifying behaviour with a template parameter like this is usually called a Policy to distinguish it from the Strategy pattern, which is exactly the same ...
Useless's user avatar
  • 12.7k
6 votes
Accepted

Can the C++ template system be used to perform type safe operations of the relational algebra

What you're talking about will not be directly possible in C++ until we get some form of both reflection introspection and reflection-based generation of types. So you're going to have to wait for a ...
Nicol Bolas's user avatar
6 votes
Accepted

Template argument type exclusion: Would this make for a useful C++ feature?

Why would it make sense to forbid specificially named types in template parameters (without a template specialization)? The only real reason I can imagine is that the forbidden type would cause some ...
Doc Brown's user avatar
  • 212k
6 votes

Is it possible to instantiate a template class at runtime?

The short answer is that C++ templates do not exist anywhere in any compiled code and are not available at runtime. Template instantiation is a code generation feature of C++, and therefore strictly ...
Ben Cottrell's user avatar
  • 12.1k
5 votes

A better way to generate plaintext content

I've done templating stuff a few times now and I always seem to quickly reach the limits of whatever template engine I happen to be using. I find the general solution is MORE templates. ie. Rather ...
Ewan's user avatar
  • 78.5k
5 votes
Accepted

MVC (templates): is it OK to call functions with arguments from the View?

Clearly how to display the datetime is a View responsibility. For example, my control wants to display the time as a clock image. Neither if the two Model strings provided allow me to do this. ...
Ewan's user avatar
  • 78.5k
5 votes

How to generate java class files with framework boilerplate in a project?

To automatically make .class files you compile .java files. To automatically make .java files you do something called code generation. If you do this the .java files will not be "source code&...
candied_orange's user avatar
4 votes

Was template meta programming really discovered "by accident"?

The following excerpt is subject to: https://creativecommons.org/licenses/by-sa/3.0/ History of TMP Historically TMP is something of an accident; it was discovered during the process of ...
rwong's user avatar
  • 16.9k
4 votes

Why doesn't C++ support covariance in STL containers like C# or Java?

C++ templates are invariant. In other words, they don't support covariance or contravariance. So, the reason, STL containers are not covariant, is because C++ doesn't support that. Note that std::...
Jörg W Mittag's user avatar
4 votes

Identify downsides: versioning auto-generated Terraform source code

I wouldn't save the generated templates to source control. But I would save them. Source control is not a document management system or a general storage system. Your generated templates should be ...
Ewan's user avatar
  • 78.5k
4 votes
Accepted

Using templates without generic implementation

Is it a good practice to declare a template class, but only with specialized implementations ? That should not be a problem. The C++ standard even has it in several places, like std::numeric_limits&...
Bart van Ingen Schenau's user avatar
3 votes
Accepted

A better way to generate plaintext content

It sounds like you need some post processing of the rendered templates. What you are after shouldn't be too hard to accomplish with regular expressions. Your first two requirements can be handled by ...
Greg Burghardt's user avatar
3 votes
Accepted

C++ generic graph search algorithm with generic data types

You don't have to directly supply the contained type to the algorithm. There are a few choices: Algorithm Template The search algorithm really only needs to know that it operates on some node type, ...
Caleth's user avatar
  • 11.5k
3 votes

What are the advantages of dynamic configuration based form templates?

None in my view. The danger with the dynamically generated forms approach is that the meta-language for defining a form becomes just as complex as the language you would use to create a hand written ...
Ewan's user avatar
  • 78.5k
3 votes

Is there a coding style for Blade templates?

This often relies on preferences. BLADE doesn't have any special coding standards that differ from standard PHP in HTML. If you work in a company, there might be some standard set to keep the code ...
dynero's user avatar
  • 141
3 votes
Accepted

What is an efficient way to implement 'Stack' without generics?

As the OP correctly stated in a comment, both of these language constructs were added to their respective langugae to enable developers to write type-independent code and still provide compile-time ...
Hulk's user avatar
  • 508
3 votes

Why doesn't C++ support covariance in STL containers like C# or Java?

In Java, generics are invariant; you would need to use bounded wildcards to achieve covariance or contravariance. To do what you want, in Java it would need to be declared like this: List<? ...
user102008's user avatar
3 votes

Using templates to define sizes of containers?

There is nothing technically wrong with this template. However, programs which use hard-coded upper limits for strings or containers tend to break the Zero-One-Infinity rule, which makes them prone to ...
Doc Brown's user avatar
  • 212k
3 votes

Using templates to define sizes of containers?

Templates for fixed sized containers is a valid practice. It fills some niche needs: the standard library offers for example std::array as a more consistent alternative to native fixed sized arrays. ...
Christophe's user avatar
  • 79.9k
2 votes
Accepted

C++ function (de)duplication in templatized classes

I don't think you can depend on this happening with current compilers. Rather than the g_aux() suggested in the comments, you might consider using inheritance: class base { public: int g() { ...
Jerry Coffin's user avatar
  • 44.6k
2 votes

where exactly should python business logic be placed in django

In the official Django docs https://docs.djangoproject.com/en/1.11/, it says: Django has the concept of “views” to encapsulate the logic responsible for processing a user’s request and for ...
Simon's user avatar
  • 137
2 votes

Handling more specialized interface with no code duplication

As you noticed, using a dynamic_cast is fairly fragile and inelegant. If some_method() and some_better_method() have the same interface (i.e. satisfy the Liskov substition principle), there is no ...
amon's user avatar
  • 135k
2 votes

When one should use template specialization, if not for metaprogramming?

When one should use template specialization, if not for metaprogramming? The "classical" thing, and even the one you mention at the end, is to refine a given generic implementation for a specific (...
Martin Ba's user avatar
  • 7,647

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