using extern template (C++11) to avoid instantiation

You should only use extern template to force the compiler to not instantiate a template when you know that it will be instantiated somewhere else. It is used to reduce compile time and object file size. For example: // header.h template<typename T> void ReallyBigFunction() { // Body } // source1.cpp #include “header.h” void something1() { … Read more

How do we use void_t for SFINAE?

1. Primary Class Template When you write has_member<A>::value, the compiler looks up the name has_member and finds the primary class template, that is, this declaration: template< class , class = void > struct has_member; (In the OP, that’s written as a definition.) The template argument list <A> is compared to the template parameter list of … Read more

Django Pass Multiple Models to one Template

I ended up modifying @thikonom ‘s answer to use class-based views: class IndexView(ListView): context_object_name=”home_list” template_name=”contacts/index.html” queryset = Individual.objects.all() def get_context_data(self, **kwargs): context = super(IndexView, self).get_context_data(**kwargs) context[‘roles’] = Role.objects.all() context[‘venue_list’] = Venue.objects.all() context[‘festival_list’] = Festival.objects.all() # And so on for more models return context and in my url(r’^$’, IndexView.as_view(), name=”home_list” ),

C++ template typename iterator

In list<tNode<T>*>::iterator, you have a dependant name, that is, a name that depends on a template parameter. As such, the compiler can’t inspect list<tNode<T>*> (it doesn’t have its definition at this point) and so it doesn’t know whether list<tNode<T>*>::iterator is either a static field or a type. In such a situation, the compiler assumes that … Read more

Class template for numeric types

You can use the std::is_arithmetic type trait. If you want to only enable instantiation of a class with such a type, use it in conjunction with std::enable_if: #include <type_traits> template< typename T, //real type typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type > struct S{}; int main() { S<int> s; //compiles S<char*> s; //doesn’t compile } For a … Read more

What is the meaning of template with empty angle brackets in C++?

template<> tells the compiler that a template specialization follows, specifically a full specialization. Normally, class A would have to look something like this: template<class T> class A{ // general implementation }; template<> class A<int>{ // special implementation for ints }; Now, whenever A<int> is used, the specialized version is used. You can also use it … Read more

Ruby templates: How to pass variables into inlined ERB?

For a simple solution, use OpenStruct: require ‘erb’ require ‘ostruct’ namespace = ‘Joan’, last: ‘Maragall’) template=”Name: <%= name %> <%= last %>” result = { binding }) #=> Name: Joan Maragall The code above is simple enough but has (at least) two problems: 1) Since it relies on OpenStruct, an access to a … Read more