Questions tagged [template-instantiation]
80 questions
3
votes
2 answers
Instantiation of friend function defined inside a template
This is a follow up of this question. The original case was something else, but in the course of me writing a poor answer and OP clarifying, it turned out that we probably need the help of a language-lawyer to understand what is going on.
In…

463035818_is_not_an_ai
- 109,796
- 11
- 89
- 185
3
votes
1 answer
Does "if constexpr(something false)" ALWAYS omit template instantiation
Will this template function f() be always NOT instantiated?
if constexpr(something false){
//some template function OR function in template class
f();
}
Below is my test (coliru MCVE).
I created fun() that will instantiate E…

cppBeginner
- 1,114
- 9
- 27
3
votes
0 answers
C++: Parallelize Template Instantiation
We have a big class with multiple template parameters. To cut down on compile time, we are already using explicit instantiatiation. While this reduces the compile time of everyone who includes the header, compiling the cpp file is still super…

mrks
- 8,033
- 1
- 33
- 62
3
votes
2 answers
Error in template instantiation before overloading
Given the following code
#include
#include
template
class Something {
public:
template
auto foo(F&&)
-> decltype(std::declval()(std::declval())) {}
template
…

Curious
- 20,870
- 8
- 61
- 146
3
votes
1 answer
Inconsistent type completeness in the destructor of a template base class
Please ignore the dubious inheritance pattern from a design point of view. Thanks :)
Consider the following case:
#include
struct Foo;
struct Bar : std::unique_ptr {
~Bar();
};
int main() {
Bar b;
}
In both GCC and Clang,…

Quentin
- 62,093
- 7
- 131
- 191
3
votes
1 answer
Declaration doesn't solve 'explicit specialization after instantiation' error
Suppose I am attempting to create my own implementation of boost::filesystem::path, using the Curiously Recurring Template Pattern:
(Code is given incomplete for brevity, but will exhibit the problem as stated when compiled with 'g++ -std=c++11 -o…

Cognitive Hazard
- 1,072
- 10
- 25
2
votes
0 answers
X is not a valid template argument for 'const char*' because it is not the address of a variable
I'm trying to use the resources of a temporary class object as a template parameter. But apparently this doesn't work:
godbolt
#include
constexpr size_t size(const char* s)
{
int i = 0;
while(*s!=0) {
++i;
++s;
…

glades
- 3,778
- 1
- 12
- 34
2
votes
1 answer
C++ function overload resolution from inside a function template depends on whether the function is defined in a namespace?
The following code compiles fine:
#include
//namespace N {
void f( int x ) { printf( "f( int ) called\n" ); }
void f( double x ) { printf( "f( double ) called\n" ); }
//}
template < typename T > inline void g( const T& t ) {
// …

Ted Law
- 21
- 1
2
votes
1 answer
Why does the compiler instantiate templates while I ask him not to?
I would like to prevent the compiler from implicitly instantiating some template with extern template.
The following snippets works as expected (the static_assert does not trigger):
template
void f() {
static_assert(sizeof(T) == 0,…

janou195
- 1,175
- 2
- 10
- 25
2
votes
1 answer
Template instantiation causing function bloating
As i started experimenting more in depth with C++1x features i ran into some thinking. For example when there is this construct
template unsigned int functionForTest(const char (&a)[N]);
and the usage of it…

Grandissimo
- 31
- 1
2
votes
1 answer
Force template instantiation via typedef template - why it works?
I am learning forcing template instantiantion.
It works, but I am still curious :-
#include
#include
template struct NonTypeParameter { };//#1#
int lala=0;
template class InitCRTP{
public: static…

javaLover
- 6,347
- 2
- 22
- 67
2
votes
0 answers
Explicity specializing a class template in source file
I have a class template with some template aliases. Since I am only using a closed set of types on the template, I would like to specialize and explicitly instantiate them. I currently have this:
// Header
template
struct Literal {
…

user975989
- 2,578
- 1
- 20
- 38
2
votes
2 answers
C++ standard requirements to templates that are not instantiated
So I tried to compile the code below and it failed (as expected):
1.cpp: In function ‘int foo()’:
1.cpp:3:5: error: ‘some’ was not declared in this scope
some ill-formed code
^
But if I remove this line the compiler compiles it without…

eXXXXXXXXXXX2
- 1,540
- 1
- 18
- 32
2
votes
1 answer
Expecting different types depending of point of instantiation
I expect the following to be ill formed NDR, but it seems not :-(
#include
template
struct is_complete : std::false_type {};
template
struct is_complete

Jarod42
- 203,559
- 14
- 181
- 302
2
votes
1 answer
Shared Library: Undefined Reference with Partial Template Specialization and Explicit Template Instantiation
Say, there is a third-party library that has the following in a header file:
foo.h:
namespace tpl {
template
struct foo {
static void bar(T const&) {
// Default implementation...
};
};
}
In the interface of my…

Alexander Shukaev
- 16,674
- 8
- 70
- 85