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
0
votes
1 answer

How to explicitly instantiate a func template with no parameter?

I have a member func template as following: using ArgValue_t = std::variant; struct Argument_t { enum Type_e { Bool, Double, Int, String, VALUES_COUNT }; template as( const Argument_t& def )…
Leon
  • 1,489
  • 1
  • 12
  • 31
0
votes
1 answer

Statically compiled library for explicit instantiatied template class with integer ranges as parameters

I have a non-type parameter template class in some header file: template class A; I want to explicitly instantiate some classes in ranges N1 = [0...K1], N2 = [0...K2], but this can result in many permutations: template…
0
votes
1 answer

Perfect forwarding fails to link with explicit template instantiation directives (EIDir; a.k.a. manual instantiation)

The question is why does the linker fail (g++, ver. 7.5, Ubuntu 18.4) to perfect forward a constructor of a template class, when the constructor definition is hidden in the implementation file (*.cpp) but explicitly instantiated in the…
0
votes
1 answer

How template explicit instantiation works?

I am struggling to understand Template Explicit Instantiation using extern specifier. Here is my example" // power.h template class Power { public: T operator()(T const& x) {return x * x;} }; // power.cpp #include…
Maestro
  • 2,512
  • 9
  • 24
0
votes
1 answer

When function templates are instantiated in a class template?

At which moment the function templates within the following class template are instantiated? // a.h #pragma once template class A { public: template void func1(T2 t); void func2(T t); T func3(); void…
j_d
  • 19
  • 1
  • 7
0
votes
0 answers

What is the most efficient way to explicitly instantiate template class for many data types?

Suppose I have a header file as follows: template class Foo { template int bar(); template int foobar(); }; I want to explicitly instantiate this class and all its methods to work with int,…
0
votes
1 answer

Explicitly instantiate templates using a tuple

To explicitly instantiate a class template I can do the following: //.h template class Foo{}; //.cpp template class Foo; This is nice because the Foo class will only have to be instantiated once now when being used from…
Andreas Loanjoe
  • 2,205
  • 10
  • 26
0
votes
0 answers

Avoid explicit instantiation in templated functions

I mean to write a templated function, which should be available for a somewhat large number of typenames. So, I mean to avoid needing explicit instantiation. In this case, is it mandatory to write the definition of the function in the header? Is…
0
votes
3 answers

Why don't default parameters work alongside a parameter pack?

Because of 8.3.6 ([dcl.fct.default])/4, In a given function declaration, each parameter subsequent to a parameter with a default argument shall have a default argument supplied in this or a previous declaration or shall be a function parameter…
0
votes
0 answers

Explicitly initialize reference using constructor with multiple arguments

Let's say I have the classes: class Rect { int w, l; Rect(width, length) : w(width), l(length) {} } class Box { Rect &r; int h; Box(int width, int length, int height); } For the sake of argument, I do not offer default values for…
Osama Kawish
  • 312
  • 1
  • 5
  • 15
0
votes
1 answer

Problem with Explicit Template Instantiation

I cannot make the following piece of code with Explicit Template Instantiation compile. I get an error on this line: std::map DW_enumDescription::descMap = std::map With clang, the error is: error: no…
Pietro
  • 12,086
  • 26
  • 100
  • 193
0
votes
1 answer

Explicit instantiation of function with auto->decltype approach

I want to create template function that would be able to extract any property of struct A. Here is Source.h struct B { int bbb; }; struct C { double ccc; }; struct A { B b; C c; }; template auto foo(A* str, R getter) ->…
Demaunt
  • 1,183
  • 2
  • 16
  • 26
0
votes
3 answers

Explicit instanciation of template linker error

I'm asking your help because I'm using templates and it seems like I misunderstood something. Here's my code : arbre.h template class Arbre; template class Noeud { friend class Arbre; public : Noeud(); …
Harkan
  • 153
  • 2
  • 12
0
votes
1 answer

Does template explicit instantiation work with forward-declared-types?

If I have a template class with some method defined in the .cpp file instead of the .h file, I can use explicit instantiation to get the compiler to avoid unresolved externals. But would it work if the explicit instantiation is declared using a…
Michel
  • 338
  • 1
  • 13
0
votes
1 answer

Virtual inheritance, explicit instantiation—returning derived class reference / pointer (covariant types)

.hpp template struct A { virtual A& modify() = 0; }; template struct B : virtual A {}; template struct C : B { C& modify() final; }; .cpp template C& C::modify() { // … …