All Questions
Tagged with function-templates language-lawyer
24 questions
4
votes
0
answers
150
views
If I forward declare a function template, may I put the definition after the calling site and not explicit instantiate it at all?
In a header file of a large project, I have to forward declare a function template before the calling site. The code boils down to this:
//H1.h
#pragma once
template <typename>
void f();
...
4
votes
1
answer
213
views
Non type template parameter in msvc does not compile
I studied about non-type template parameter and came to know that they must be compile time constant. Then I created the below given program that compiles with gcc as well as clang but fails to ...
2
votes
1
answer
91
views
operator== compiles with msvc but not with gcc and clang
I am learning C++ using the books listed here. Now, to further check that I've understood the concepts I'm also writing simple sample programs. One such program that compiles with msvc but does not ...
2
votes
1
answer
136
views
Function template argument deduction works in gcc but not in msvc and clang
I am learning C++ templates using the resource listed here. In particular, read about template argument deduction. Now, after reading, to further clear up my concept of the topic I tried the following ...
1
vote
1
answer
222
views
MSVC not able to disambiguate between function templates when one of them contains a pack
Recently I reported a msvc bug involving a function parameter pack. Also as it turns out here that msvc is actually standard compliant there.
Then when I modified the example to what is shown below I ...
5
votes
1
answer
242
views
MSVC vs Clang/GCC bug during overload resolution of function templates one of which contains a parameter pack
I was using parameter pack when I noticed that one such case(shown below) compiles fine in gcc and clang but not in msvc:
template<class T> void func(T a, T b= T{})
{
}
template<class T, ...
2
votes
3
answers
2k
views
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<...
0
votes
3
answers
157
views
Moving the function templates definition to different translation unit resolves the ambiguity error
I was using function templates when I noticed that moving the definition of one of the function template to a different translation unit resolves the ambiguous error. Below are the two examples that ...
1
vote
1
answer
262
views
GCC & Clang vs MSVC Bug while expanding template parameter pack in the same parameter clause for function templates
I came across the following statement in the standard:
If a template-parameter is a type-parameter with an ellipsis prior to its optional identifier or is a parameter-declaration that declares a pack ...
5
votes
1
answer
107
views
Does "used as non-type template parameter" make a function template implicitly instantiated?
I want to write a class template M that accepts incomplete type C as template parameter.
But I also want C has some traits when it is eventually defined.
Is this code guaranteed
to compile if defined(...
2
votes
0
answers
558
views
C++ template function, counting all template class specializations exactly before its point of instantiation
Suppose, we have
template <int> struct Node;
It is required to get "reusable" compile-time function, which counts all Node specializations, defined exactly before every call of that ...
1
vote
1
answer
211
views
C++ constexpr function template compiles with non-literal and non-aggregate types. Is this a bug with gcc? [duplicate]
Consider the following code:
#include <iostream>
#include <cstdlib>
struct NonAggregate
{
// Non-aggregate due to user defined constructor
// It is also not constexpr
explicit ...
0
votes
1
answer
85
views
is it legal for an auto variable in a function to represent a different type in different invocations?
Consider the following function:
using iteratorPair = std::pair<std::set<Record>::const_iterator, std::set<Record>::const_iterator>;
using iteratorList = std::vector<iteratorPair&...
5
votes
1
answer
189
views
How to understand the rules of partial ordering about T& and T const&
template <typename T>
void show(T&); // #1
template <typename T>
void show(T const&); // #2
int main()
{
int a = 0;
show(a); // #1 to be called
}
I'm ...
6
votes
1
answer
210
views
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 <bool A, typename X>
void f (X &x) {} // 1
template &...
2
votes
2
answers
204
views
Is deduction of multiple template arguments in an explicit specialization of a function template allowed?
The following quote from [temp.expl.spec.11]:
A trailing template-argument can be left unspecified in the template-id naming an explicit function template specialization provided it can be deduced ...
4
votes
1
answer
252
views
template overloading results in linker error / strange behaviour
with the following minimal example i get a linker error on my local system in visual studio 15.8.7 (standard console app with standard settings (just removed precompiled headers)): "Error LNK1179 ...
1
vote
1
answer
530
views
How does C++1z standard define the correct location of attributes in a template function declaration?
I noticed that the section 18.8/1 in C++11 and C++14 standards contained the following declaration in the <exception> header synopsis:
[[noreturn]] template <class T> void ...
6
votes
3
answers
271
views
Inconsistency in function type decay between variadic/non-variadic templates?
Given a non-variadic function template:
template<class T>
void f(void(t)(T));
And some plain functions:
void f1(int);
void f2(char);
This works:
f(f1);
The type of t becomes void (*)(int).
...
8
votes
1
answer
575
views
constexpr static template function: g++ error is a warning on clang
Consider the following snippet:
#include <iostream>
template <int I>
constexpr int f() { return I * f<I-1>(); }
template<>
constexpr int f<0>() { return 1; }
int main ...
15
votes
2
answers
1k
views
Why default argument can't be added later in template functions?
C++ standard section 8.3.6.4 says that
For non-template functions, default arguments can be added in later
declarations of a function in the same scope. [...]
But my question is that why it isn'...
5
votes
2
answers
1k
views
Compilation issue with instantiating function template
Consider the following code:
#include <iostream>
struct S {
void f(const char* s) {
std::cout << s << '\n';
}
};
template <typename... Args, void(S::*mem_fn)(Args...)&...
3
votes
2
answers
151
views
Perfect forwarder in Herb Sutter's C++Con 2014 talk
In Herb Sutter's talk at C++Con 2014, among other things he discusses passing by value, by reference, and so forth. One technique he presents in this albeit contrived example is:
using namespace std;
...
5
votes
1
answer
878
views
Specialization of template function after point of use will break the compilation
Consider next example :
#include <iostream>
template< int a >
void foo();
int main(int argn, char* argv[])
{
foo<1>();
}
template<>
void foo<1>()
{
std::cout&...