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

Clang++ makes linker fail on template classes (but it works with g++)

I have a program that works nicely with GCC, but compiling it with Clang instead makes the linker fail. I think my issue is with template classes, so I implemented this small example. test.cpp: #include "Point.h" int main() { Point p1; …
scozy
  • 2,511
  • 17
  • 34
3
votes
2 answers

Function not visible durring instantiation of variadic templates

I am trying to make a function that returns zero filled array of array of array... Following Elegantly define multi-dimensional array in modern C++ I defined: template
Pana
  • 121
  • 7
3
votes
1 answer

Instantiation of function templates

In the following program we take the address of a function template for which no definition is available. template void fun(T); int main() { void (*funptr)(int) = fun; } I was taught that the compiler decides to instantiate a…
Jasper Koning
  • 143
  • 1
  • 9
3
votes
1 answer

Undefined reference to explicitly instantiated template function

I have a class with a template method that is declared in a header and defined in a cpp file, so // A.h - declaration struct A { template void fun(); }; and then // A.cpp - definition #include #include…
3
votes
1 answer

Instantiate member function templates for multiple types

I have a several classes with templated member functions and a predefined list of types they will be used with (Wandbox link: // main.cpp: #include "class.h" int main(int argc, char** argv) { A a; a.foo(5); a.foo(5.); B b; …
tstenner
  • 10,080
  • 10
  • 57
  • 92
3
votes
2 answers

Conditionally remove methods in template class

Background: The usage of STL makes builds slow due to template code bloat: quite often the same methods are independently instantiated in many different translation units, being compiled and optimized many times. One way to avoid this object code…
stgatilov
  • 5,333
  • 31
  • 54
3
votes
1 answer

Why can explicit instantiation of a function template not use inline or constexpr

Referring to cppreference's section on function templates: Explicit instantiation of a function template or of a member function of a class template cannot use inline or constexpr These topics, inline and constexpr, seem separate and unrelated.…
lsbbo
  • 304
  • 3
  • 12
3
votes
0 answers

C++ template explicit instantiation declarations: best practices

My question is how we should use template explicit instantiation declarations in a right way? Suppose we have some template class template Foo. Here is my mind how to deal with this feature: We put template declaration in Foo.h file,…
3
votes
0 answers

defaulted constructors not produced in explicit instantiation

I don't understand why C++ seems to suppress generation of defaulted ctors in an explicit template instance. For this source file, template class C { public: C() = default C(C const&) = default; C(VAL const val) : val_(val)…
3
votes
2 answers

Call of explicitly instantiated template function through conversion operator

Let us assume we have a function template which is implemented in the cpp file with help of explicit instantiation like this: function.h template void function(T val); function.cpp #include "function.h" template void…
3
votes
1 answer

template template function instantiation

template < typename T > class CLASS_TEMPLATE { } ; template < template < typename T > class CLASS > void funcI ( ) { } template void funcI < CLASS_TEMPLATE > () ; How does compiler instantiate the function, if he does not have any hint about…
Volodymyr Boiko
  • 1,533
  • 15
  • 29
3
votes
1 answer

C++ template explicit instantation, with template argument being a class template

I have a linker issue when trying to explicitly instantiate a class. Using C++11, LLVM 5.1 . Here is a minimal working example: declaration.h : template class Box { public : template using Box_sub =…
brahmin
  • 568
  • 1
  • 5
  • 17
3
votes
2 answers

C++ member function explicit template instantiation across DLL

I am creating a DLL in C++ using Visual Studio 2013 on Windows 8.1 Update 1. There is a class called XMLData which has a public member function called getAttribute. XMLData.h namespace DDGL { class DLL_EXPORTED XMLData { ... …
OMGtechy
  • 7,935
  • 8
  • 48
  • 83
3
votes
2 answers

Separate compilation and template explicit instantiation

Summary This question is about achieving separate compilation of a single template class instantiation in a several different translation units. Question For non-template classes one can put definitions in several .cpp files and compile them…
Vayun
  • 51
  • 5
2
votes
1 answer

Why can (implicitly) instantiated function templates use undeclared symbols?

I have the following code: template void fun(T t) { // foo and bar are not declared yet, but this is okay, // because they can be found through ADL for a class type T foo(t); bar(t); } struct A {}; void foo(A); //…
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
1 2
3
9 10