Questions tagged [template-instantiation]
80 questions
6
votes
0 answers
Reliably check for whether a class template has been instantiated, with C++11?
It used to be possible, using some C++ compilers, to check whether a templated type has already been instantiated, so that the following program syntax compiles without error:
template struct MyStruct { };
// some magic goes here
int…

einpoklum
- 118,144
- 57
- 340
- 684
6
votes
1 answer
Template parameter type is treated as complete by the compiler, but its definition isn't yet visible
Assume I have the following code snippet:
template
class Bar
{
// static_assert(sizeof(T) > 0); // (1)
public:
void method()
{
static_assert(sizeof(T) > 0); // (2)
}
};
class Foo; // (3)
template class Bar;…

undermind
- 1,779
- 13
- 33
6
votes
2 answers
Is a virtual function of a template class implicitly instantiated?
Consider the following code. Is it guaranteed that Derived::foo() will be instantiated? foo() is virtual and is called by a non-virtual function of the base class.
#include
class Base
{
public:
void bar() { foo(); }
private:
…

BlindDriver
- 627
- 3
- 14
6
votes
2 answers
type deduction failing for auto stdMaxInt = std::max;
Using GCC 4.8.4 with g++ --std=c++11 main.cpp outputs the following error
error: unable to deduce ‘auto’ from ‘max’
auto stdMaxInt = std::max;
for this code
#include
template
const T& myMax(const T& a, const T& b)
{
…

Tobias Hermann
- 9,936
- 6
- 61
- 134
5
votes
1 answer
Origin story of [temp.spec]/6?
[temp.spec]/6 reads:
The usual access checking rules do not apply to names in a declaration of an explicit instantiation or explicit specialization, with the exception of names appearing in a function body, default argument, base-clause,…

Andrew Tomazos
- 66,139
- 40
- 186
- 319
5
votes
2 answers
Referring to specific template specialization from a non-instantiated context: instantiation or not?
Consider the following example
template struct S
{
A a;
void foo() {}
};
template void bar()
{
S *p = 0;
}
template void baz()
{
S{}.foo();
}
template void qux()
{
…

AnT stands with Russia
- 312,472
- 42
- 525
- 765
5
votes
1 answer
Optionally safety-checked cast on possibly incomplete type
Pursuant to a simple, intrusively reference-counted object system, I have a template class Handle, which is meant to be instantiated with a subclass of CountedBase. Handle holds a pointer to a T, and its destructor calls DecRef…

Sneftel
- 40,271
- 12
- 71
- 104
4
votes
1 answer
Mechanism to prevent instantiations of a function template
I would like to declare a function per typename, so that I can refer to it by type. Critically, I would like to do this even for nameless lambda types. The classic way to do this is of course a template:
template
void template_foo()…

George Hilliard
- 15,402
- 9
- 58
- 96
4
votes
0 answers
Using trailing return type to prevent function template instantiation?
While toying with crude reproductions of some of C++17 if constexpr functionalities, I encountered an issue where using auto seems to trigger the instantiation of a template.
The function template in question contains an assertion which fails as…

Etienne M
- 604
- 3
- 11
4
votes
1 answer
Difficulty of implementing `case` expressions in a template-instantiation evaluator for a lazy functional language?
I'm following "Implementing functional languages: a tutorial" by SPJ, and I'm stuck on Exercise 2.18 (page 70), reproduced below. This is in the chapter about a template-instantiation evaluator for the simple lazy functional language described in…

Jonathan Lam
- 16,831
- 17
- 68
- 94
4
votes
0 answers
Point of instantiation in complete-class context
template
struct A {
using U = typename T::U;
using V = typename T::V; //X
};
struct B {
using U = int;
void f() { A a; } //1
//A a; //2
using V = int;
};
This compiles on current GCC, Clang,…

user17732522
- 53,019
- 2
- 56
- 105
4
votes
0 answers
GCC 6.1 explicit template instantiation gives undefined symbol for default constructor
Put this in a file called t.hpp:
#include
extern template class std::unordered_map;
std::unordered_map getMap();
And this in t.cpp:
#include "t.hpp"
std::unordered_map getMap()
{
return…

John Zwinck
- 239,568
- 38
- 324
- 436
3
votes
0 answers
error: static assertion failed: template argument must be a complete class or an unbounded array
I'm deriving from a base class in which I try to define a type. That type is dependent on itself via a variant, so it would require to know the memory layout of the base class upon definition. However, I'm not prematurely defining the type? So I…

glades
- 3,778
- 1
- 12
- 34
3
votes
2 answers
If a function definition has a parameter of class template type and didn't use it (its members) then is it instantiated?
From the previous example I've posted here about when the template is instantiated?, I got the answer that only when a template is used the compiler instantiates it. But look at this example:
template
struct Pow{
T operator()(T…

Itachi Uchiwa
- 3,044
- 12
- 26
3
votes
2 answers
How do I properly implement "operator()" with "if constexpr" so that it works with std::generate?
I was trying to write a class that will fill a container with random numbers with the type that the container has:
template
class rand_helper {
private:
std::mt19937 random_engine;
std::uniform_int_distribution…

Alpharius
- 489
- 5
- 12