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
2
votes
0 answers

Nested class, dllexport and VS2010

Let's have the following code: template struct X { X() { } }; struct __declspec(dllexport) A { struct __declspec(dllexport) AB { int i; }; typedef X XAB; //template struct __declspec(dllexport) X ; // error C2252: an…
2
votes
0 answers

"definition of explicitly-defaulted" error with explicit template instantiation definition (Clang VS GCC)

Consider the following code: template struct S { S(); }; // extern template struct S; template S::S() = default; template S::S(); clang++ happily accepts this code. g++ complains with: error: definition of…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
2
votes
3 answers

Are explicit template instantiation definition for a function template allowed in header files

I was reading about explicit template instantiation when i came across the following answer: Assuming by "explicit template instantiation" you mean something like template class Foo; // explicit type instantiation // or template void…
2
votes
1 answer

Should explicit instantiation of templates be used if I want to limit what types a class template can accept?

I have a templated class, and I want to only allow certain types to be used. I believe one way to do this is to use explicit instantiation at the end of the source file -- I believe this will cause linker errors if the developer tried to instantiate…
David
  • 619
  • 2
  • 8
  • 15
2
votes
1 answer

Does static data member specialization in CRTP violate ODR?

First of all, I know this question might seem a duplicate. But I've read many posts with a similar question and didn't find an answer. Second of all, I haven't had any issue with my solution so far. So I'm asking rather if my solution is a correct…
LRDPRDX
  • 631
  • 1
  • 11
  • 23
2
votes
1 answer

extern template declaration with alias payload

Consider this. There is a class Derived that inherits from some instantiation of a heavy templated class Base, and there are many uses of Derived in various source files. So it is reasonable to have only one Derived-specific instantiation of…
Vahagn
  • 4,670
  • 9
  • 43
  • 72
2
votes
1 answer

How do I portably export template instantiations from a shared library

I have this header: #pragma once #if _MSC_VER # ifdef mylib_EXPORTS # define MYLIB_EXPORT __declspec(dllexport) # else # define MYLIB_EXPORT __declspec(dllimport) # endif #else # ifdef mylib_EXPORTS # define MYLIB_EXPORT…
2
votes
1 answer

Type grouped Explicit Instantiation of templates

If I have a template class with overloaded template member functions(using SFINAE) like: template struct Foo{ Foo(T elem); template auto get() -> std::enable_if_t, U>; template…
tangy
  • 3,056
  • 2
  • 25
  • 42
2
votes
1 answer

LNK2019 error when referring an explicit instantiation of a function template in another VS project

I'm experiencing a LNK2019 linker error when trying to reference a class with a explicitly instantiated function template into another Visual Studio 2013 project. The explicit instantiations works inside the same project (project1). But as soon as…
James
  • 270
  • 1
  • 10
2
votes
1 answer

MSVC fails to compile an explicit template instantiation with a private type as an argument

Consider the following example, consisting of 4 files. Outer.h #pragma once #include "genericAlgorithm.h" class Outer { private: struct Inner {}; // Note that Inner is private const Inner inner; public: Outer() : inner() {} …
TerraPass
  • 1,562
  • 11
  • 21
2
votes
1 answer

Explicit template instantiation and forwarding references

Recently, I was discussing with students the possibilities of restriction of types for templates that uses forwarding references. I knew about comparing types by is_same together with static_assert or enable_if, but we also talked about explicit…
Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
2
votes
1 answer

When do we need to explicitly instantiate a template function?

Let's say we have a template function: template T max(T a, T b) { return a > b ? a : b; } Since the compiler doesn't perform any implicit type conversion during template argument deduction, we can invoke max(2, 5.5) in these two…
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
2
votes
2 answers

C++ explicit instantiation of template constructor of non-template class

I'm working on a C++ Fraction class that use pimpl idiom, my public header is something like (work in progress) Fraction.h code: #pragma once #include #include class Fraction { public: Fraction(); ~Fraction(); …
MaxC2
  • 343
  • 3
  • 10
2
votes
1 answer

Explicitly instantiating variadic constructor: template-id does not match any template declaration

I'm trying to explicitly instantiate a variadic constructor. This minimal example to print all arguments causes the same error I'm seeing on MinGW-w64 on 64 bit Win 7 with GCC 5.3. struct stf { template stf(Args&&... args){ …
2
votes
0 answers

Explicit template instantiation and SFINAE

I am getting a compilation error so clearly I am doing something wrong but I am not sure how to fix it nor if it is possible to have explicit instantiation and SFINAE. The reproducible example is minimal so it might not make complete sense but…
xerion
  • 303
  • 2
  • 11