Questions tagged [template-instantiation]
80 questions
21
votes
1 answer
At which point occurs template Instantiation binding?
This code is from "C++ programming language" by Bjarne Stroustrup (C.13.8.3 Point of Instantiation Binding)
template
void f(T value)
{
g(value);
}
void g(int v);
void h()
{
extern g(double);
f(2);
}
And he mentions:
Here,…

user1289
- 1,251
- 2
- 13
- 25
18
votes
1 answer
Understanding declval optimized implementation
Looking at libstdc++ source code, I found the following declval implementation:
template
_Up __declval(int); // (1)
template
_Tp __declval(long); // (2)
template
auto declval()…

Igor R.
- 14,716
- 2
- 49
- 83
16
votes
1 answer
Why does = default member initializer request instantiation of unique_ptr destructor while {} does not?
This is a follow up of this question: Does PIMPL idiom actually work using std::unique_ptr?
The full example uses multiple files, so for the sake of this question I will reduce it here. The full working example is here:…

463035818_is_not_an_ai
- 109,796
- 11
- 89
- 185
12
votes
1 answer
Is use in an unused default member initializer still an odr-use?
Is use in a default member initializer still an odr-use, even if the default member initializer is not used by any constructor?
For example, is this program ill-formed because g is odr-used and therefore its definition implicitly…

user17732522
- 53,019
- 2
- 56
- 105
11
votes
2 answers
Is it ok to define a function using SFINAE that requires non-existance of the function?
The code in this question is based on this answer. I am a little confused about how this produces the output it does, and whether it is all well defined
#include
#include
#include
struct bar {};
void foo(bar)…

463035818_is_not_an_ai
- 109,796
- 11
- 89
- 185
11
votes
1 answer
explicit instantiation of function using decltype : work on g++ but not on Visual C++
This codes run on G++, but not on Visual C++.
#include
template void foo( T& t,int some_parameter){}
template decltype(foo) foo;
int main(){
std::cout << "Hello, world!\n";
}
Here is the error from Visual C++…

cppBeginner
- 1,114
- 9
- 27
11
votes
4 answers
Force template instantiation via typedef : success at g++ , fail at Visual C++
I want to force template instantiation.
The below code works (print 1) at g++ ( http://coliru.stacked-crooked.com/a/33986d0e0d320ad4 ).
However, it prints wrong result (0) at Visual C++ ( https://rextester.com/WGQG68063 ).
#include…

javaLover
- 6,347
- 2
- 22
- 67
11
votes
4 answers
Is it possible that a compiled program does not contain an instantiated template class?
Consider this code:
template
class A {
T x;
// A bunch of functions
};
std::size_t s = sizeof(A);
Assume the sizeof operator is the only place where an instantiation of A is required. Is it possible that the…

iBug
- 35,554
- 7
- 89
- 134
10
votes
1 answer
Is a specialization implicitly instantiated if it has already been implicitly instantiated?
The question in the title is clear enough. To be more specific, consider the following example:
#include
template
struct is_complete_helper {
template
static auto test(U*) ->…

xskxzr
- 12,442
- 12
- 37
- 77
8
votes
1 answer
Is "if constexpr" useful outside of templates?
I'm trying to understand if constexpr fully.
I understand, that if if constexpr(expr) used in a template, and expr is dependent on a template parameter, then during instantiation, only one of the then/else branches will be instantiated, the other…

geza
- 28,403
- 6
- 61
- 135
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
8
votes
2 answers
Can I make my compiler use fast-math on a per-function basis?
Suppose I have
template void foo(float* data, size_t length);
and I want to compile one instantiation with -ffast-math (--use-fast-math for nvcc), and the other instantiation without it.
This can be achieved by instantiating…

einpoklum
- 118,144
- 57
- 340
- 684
7
votes
2 answers
When a template is instantiated?
The fact that a template is not instantiated until it is used so for example if I have this class template:
template
struct Pow{
T operator()(T const& x) const{ return x * x; }
};
void func(Pow); // Pow instantiated…

Itachi Uchiwa
- 3,044
- 12
- 26
7
votes
1 answer
How template explicit instantiation works and when?
Here is an exercise from C++ primer 5th edition:
"Exercise 16.26: Assuming NoDefault is a class that does not have a default constructor, can we explicitly instantiate vector? If not, why not?"
Here is my guess:
Yes we can instantiate…

Maestro
- 2,512
- 9
- 24
7
votes
1 answer
Do typedef and using cause a template instantiation?
Say I have a template class defined like this
template
class Temp{
// irrelevant
};
I can either implicitly or explicitly instantiate it:
Temp ti;
template class Temp;
With explicit instantiation, my program should…

iBug
- 35,554
- 7
- 89
- 134