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

How to use extern template

I've been looking through the N3291 working draft of C++0x. And I was curious about extern template. Section 14.7.3 states: Except for inline functions and class template specializations, explicit instantiation declarations have the effect of…
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
21
votes
6 answers

How to explicitly instantiate a template for all members of MPL vector in C++?

Consider the following header file: // Foo.h class Foo { public: template void read(T& value); }; I want to explicitly instantiate the Foo::read member function template in a source file for all types included in a…
Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
21
votes
4 answers

When would you use template explicit instantiation?

I've just been reading about template explicit instantiation: template struct MyStruct; It was described as "quite rare", so under what circumstances would it be useful?
Mark Ingram
  • 71,849
  • 51
  • 176
  • 230
16
votes
1 answer

Explicitly instantiate class through template alias

Is it possible to explicitly instantiate a template class through a template alias? If so, how? Otherwise, can someone point to the ISO paper in which this was discussed and decided against? template struct A { }; /// Explicit instantiate…
gnzlbg
  • 7,135
  • 5
  • 53
  • 106
12
votes
1 answer

Explicit instantiation of templated constructor for template class

I am uncertain if it is a bug in Clang 3.2 or a violation of C++03, but it appears that explicit instantiation of templated constructors for template classes fails, but explicit instantiation of templated member functions of template classes…
Jack Poulson
  • 1,305
  • 1
  • 10
  • 13
11
votes
1 answer

Using `extern template` with third-party header-only library

I am using the glm library, which is a header-only collection of math utilities intended for 3D graphics. By using -ftime-trace on Clang and ClangBuildAnalyzer, I've noticed that a lot of time is being spent instantiating glm types: **** Templates…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
10
votes
5 answers

How can I concisely write a lot of explicit function template instantiations?

I'm writing a C++ library that contains a lot of function templates I want to explicitly instantiate and export for several type parameters. In my particular case, I have a lot of numeric function templates that I want to separately instantiate and…
David Zhang
  • 4,314
  • 1
  • 14
  • 18
10
votes
1 answer

Template static definition and explicit specialization instantiation errors in MSVC

I'm wondering why the following code runs just fine in gcc #include using namespace std; template struct F { static T const value; }; template<> struct F { // Specialization static int const value; };…
9
votes
2 answers

Constrained member functions and explicit template instantiation

G++ and Clang++ agree that the following snippet is not valid C++: template struct Tensor {}; template double InnerProduct(Tensor const &, Tensor const &) { return 0.0; } template double…
metalfox
  • 6,301
  • 1
  • 21
  • 43
9
votes
1 answer

How to instanciate base template classes explicitly?

This question is considering explicit instanciation of template classes. Consider a template class B derived from another template class A. I want to explicitly instanicate B because its methods are to be called from dynamic linking, so the…
gexicide
  • 38,535
  • 21
  • 92
  • 152
8
votes
1 answer

C++20 Concepts: Explicit instantiation of partially ordered constraints for member functions

This works and outputs "1", because the function's constraints are partially ordered and the most constrained overload wins: template struct B { int f() requires std::same_as { return 0; } int f() requires…
unddoch
  • 5,790
  • 1
  • 24
  • 37
8
votes
1 answer

clang fails to generate defaulted move constructor upon template instantiation

The following code (I couldn't make a shorter MVCE) unit.h: #include template struct foo { std::vector data; foo(foo&&) = default; // no assembly generated foo(std::vector&&v) : data(std::move(v))…
Walter
  • 44,150
  • 20
  • 113
  • 196
7
votes
0 answers

Explicit instantiation of member function template of class template

Supposing I have a class template in my header file with a member function template. //file.hxx template struct A { T val; template foo(U a); }; and I have in a .cpp the implementation of foo: //file.cpp #include…
Teloze
  • 279
  • 2
  • 8
6
votes
1 answer

How strictly function template explicit instantiation rules are defined?

Note: this question is about explicit instantiation, not explicit specialization. Please take a look at the following example: template void f (X &x) {} // 1 template void f (int &x) {} // 2 template void f (int…
6
votes
2 answers

Why does explicit template instantiation not break ODR?

This question arised in the context of this answer. As I would expect, this translation unit does not compile: template int getNum() { return Num; } template int getNum<0>(); template int getNum<0>(); // error: duplicate explicit…
jdehesa
  • 58,456
  • 7
  • 77
  • 121
1
2 3
9 10