Questions tagged [explicit-instantiation]

Explicit instantiation lets you create an instantiation of a C++ templated class or function without actually using it in your code.

Explicit instantiation lets you create an instantiation of a C++ templated class or function without actually using it in your code.

It is designed to optimize template libraries usage providing some of (mostly used) template instances in compiled binary form instead of source code form. This will reduce compile and link time for end-user applications.

Also it could be used to encapsulate template function definition in translation unit instead of included header.

136 questions
6
votes
1 answer

Can I use decltype() to avoid code duplication in explicit template instantiations?

I have a long template function declaration: template void foo(lots ofargs, goin here, andeven more, ofthese arguments, they just, dont stop); with no overloads. and I want to explicitly instantiate it. I can write (say for T =…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
6
votes
2 answers

Initialization of a static member inside a template

Here's a minimal example: #include struct B { B() { x = 42; } static int x; }; int B::x; template struct A { int foo() { return b.x; } static B b; }; template B A::b; //template struct A<2>; // explicit…
vukung
  • 1,824
  • 10
  • 23
5
votes
1 answer

Error using `extern template` in the presence of an explicit specialization of a class template member function

Consider a class template S: [s.hpp] template struct S { void f() { /* ... */ } void g() { /* ... */ } }; This class template also comes with a source file containing a specialization of S::g(): [s.cpp] template <> void…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
5
votes
3 answers

Linking/compile time concerning static template libraries

It seems to be a common convention not to use source files for template based classes (STL and boost) and to put the implementation into the header as well. I assume that this will increase the time it takes to compile the source files that include…
Byzantian
  • 3,208
  • 4
  • 27
  • 31
4
votes
1 answer

How to make explicit instantiation of template constexpr variable in C++?

If one has a template constexpr variable (e.g. for computing Fibonacci sequence) and would like to instantiate it for some template argument, must constexpr keyword be repeated during instantiation? template constexpr size_t fib = fib +…
Fedor
  • 17,146
  • 13
  • 40
  • 131
4
votes
2 answers

Automating explicit template instantiation

To reduce compile times in a template-heavy project, I'm trying to explicitly instantiate many templates in a separate compilation unit. Because these templates depend on enum class members, I'm able to list all possible instantiations. I want all…
4
votes
0 answers

Less repetitious way to do explicit template instantiation (ETIs)? (without macros)

Small example problem: // foo.h template struct Foo { template void Bar (); }; Both S and T can be either char or int. 'Seems like an easy list to maintain, but here's the code: // foo.cpp template
Elliott
  • 2,603
  • 2
  • 18
  • 35
4
votes
1 answer

Explicit C++ template instantiation with clang

Note: Several related questions (e.g., this one) ended up being marked as duplicates of this question. I am aware of this particular question and follow the solution in the corresponding answers. However, different compilers yield different behavior…
carlosvalderrama
  • 465
  • 1
  • 6
  • 22
4
votes
1 answer

When are incomplete types okay during explicit instantiation?

I'm trying to make a kind of wrapper class which automatically creates a wrapped object: #include #include template class Foo { std::unique_ptr _x; public: Foo(); // will initialize _x }; Furthermore,…
jtbandes
  • 115,675
  • 35
  • 233
  • 266
4
votes
1 answer

C++11 extern templates: where do we actually need them?

In C++03 we have template explicit instantiation definitions (template class Foo) which force instantiation of a template class. In C++11 we've got template explicit instantiation declarations (extern template class Foo) which should…
Dmitry Katkevich
  • 883
  • 7
  • 26
4
votes
1 answer

How to export a class derived from an explicitly instantiated template in Visual Studio?

In my DLL, I have a class template and a second class derived from an instantiation of that template. Both classes shall be exported and usable in other DLLs. The compiler is Visual Studio 2013. I want the template code to be instantiated in exactly…
4
votes
1 answer

How to properly explicitly instantiate a template class with fully-specialized members?

Let's say we have the following files: foo.h namespace ns { template class Foo { public: Foo(); ~Foo(); void DoIt(); }; } foo.cpp #include "foo.h" #include namespace ns { …
Bwmat
  • 4,314
  • 3
  • 27
  • 42
4
votes
2 answers

Meaning of the following template function?

#define Create_Function(Type) \ template void Function( std::vector>&) Create_Function(std::string); I have seen the above code in legacy code but have no idea what the meaning of it. It is neither a regular…
q0987
  • 34,938
  • 69
  • 242
  • 387
3
votes
2 answers

Generate explicit instantiations with multiple parameters with preprocessor

In my project, I want to have a bunch of explicit instantiations of my templated functions to reduce build time. Now I have a lot of functions, which can have different templates. For this reason (and in case I want to have more of them) I do not…
3
votes
2 answers

When does instantiation happens for explicit instantiation of a function template

Hi i am learning about explicit instantiation. And so reading different examples but in one example have some doubts. The example is given below and i have 2 doubts in this particular example. File Application.cc contains: extern template int…
Jason
  • 36,170
  • 5
  • 26
  • 60
1
2
3
9 10