Is it possible to have a constructor template with no parameters?

There is no way to explicitly specify the template arguments when calling a constructor template, so they have to be deduced through argument deduction. This is because if you say: Foo<int> f = Foo<int>(); The <int> is the template argument list for the type Foo, not for its constructor. There’s nowhere for the constructor template’s … Read more

template argument deduction/substitution failed, when using std::function and std::bind

To figure out the problem let separate statements: auto f = bind(&TestA::testa, &testA, _1, _2); // OK test.setCallback(f); // <<— Error is here setCallback needs to know type of T and it can’t deduce it from f, so give it a type test.setCallback<TYPE>(f); // TYPE: int, float, a class, …

GCC(/Clang): Merging functions with identical instructions (COMDAT folding)

Neither GCC nor Clang is a linker, and ICF needs to be done by the linker, or at least with cooperation with the linker. Edit: They don’t do ICF, so yes, distinct instantiations produce distinct code. The GNU gold linker supports ICF with the –icf option, which needs the GCC option -ffunction-sections to be used. … Read more

What’s the right way to specialize a template when using “extern template”?

extern template class A<long>; This line says that A<long> is to be explicitly instantiated according to the definitions the compiler has already seen. When you add a specialization later, you break that meaning. Add a declaration of your specialization to the header file. template <typename T> struct A { /*…*/ }; template<> int A<long>::get() const; … Read more

Creating an interface for an abstract class template in C++

You are actually attempting the impossible. The very heart of the matter is simple: virtual and template do not mix well. template is about compile-time code generation. You can think of it as some kind of type-aware macros + a few sprinkled tricks for meta programming. virtual is about runtime decision, and this require some … Read more

std::vector as a template function argument

The right way for a template function to accept any std::vector by const& is: template<typename T, typename A> void some_func( std::vector<T,A> const& vec ) { } the second argument is the “allocator”, and in some advanced usage of std::vector it will not be the default one. If you just accept std::vector<T>, your some_func will reject … Read more

Get types of C++ function parameters

This syntax is slightly different. First, because types are easier to work with than packs, a type that holds a pack. The using type=types; just saves me work in the code that generates a types: template<class…>struct types{using type=types;}; Here is the workhorse. It takes a signature, and produces a types<?…> bundle containing the arguments for … Read more