Questions tagged [template-argument-deduction]

Template argument deduction is a compiler attempt to deduce template arguments when some are omitted.

692 questions
5
votes
1 answer

Is it legal to use an unexpanded parameter pack as the type of a template template parameter's non-type template parameter?

gcc and clang disagree about whether the following code should compile: template struct tuple {}; template struct Test; template < typename... Types, template typename... Outer, // XXX Types...…
5
votes
1 answer

Class template argument deduction - why does it fail here?

Why does the following CTAD attempt fail to compile ? template struct C { C(T,T) {} }; template <> struct C { C(int) {} }; C c(1); //error: template argument deduction failure I would have expected that the constructor C(int)…
5
votes
1 answer

How to specify template deduction guides for template aliases?

I am trying to make this code compile in VS 2019 (16.10.4) with /std:c++17. The following fails: namespace my { template, class _Allocator = std::allocator<_Key>> using Set = std::set<_Key,…
Rahul Bhobe
  • 4,165
  • 4
  • 17
  • 32
5
votes
0 answers

Nested template class argument deduction differences between compilers

The below example compiles fine in both gcc and msvc: template struct Foo { template struct Bar { Bar(T val): val_(val) { } T val_; }; }; auto func() { …
5
votes
2 answers

Why can't I deduce one of the class template arguments?

I have some code here template struct foo { public: foo(const funcType& func) : m_func(func) {} ~foo() {} void m_call() { m_func(); } private: const funcType& m_func; T…
5
votes
0 answers

C++17 Deduction Guide defined inside a class is not effective inside this class, but useful outside the class

#include struct A { }; struct B { }; struct Test { template struct overloaded : Ts { }; // 1st Deduction Guide template overloaded(Ts)->overloaded; // 2nd Deduction Guide for class…
5
votes
4 answers

Why is no deduction for template parameters only used as return type?

If I do not use tempate parameter (type) in a function argument list -> only as return type, then there is no deduction: template T zero() { return 0; } int main() { int x = zero(); } gives: a.cpp:15:18: error: no matching…
milanHrabos
  • 2,010
  • 3
  • 11
  • 45
5
votes
1 answer

How to understand the rules of partial ordering about T& and T const&

template void show(T&); // #1 template void show(T const&); // #2 int main() { int a = 0; show(a); // #1 to be called } I'm confused about these partial ordering rules. Here are some quotes:…
5
votes
2 answers

How to deduce the return type of a lambda?

I want to mimic Ruby's map() method in C++. I am struggling to figure out the return type automatically: #include #include #include #include typedef std::string T2; template
5
votes
2 answers

deduction guides for matrix

I'm looking for deduction guide for initialize matrix. I've tried to use pack in pack and sizeof..., initializer_list>, custom classes for arrays for construct, but nothing is works... so, i'm looking for initialize template…
naufumi
  • 65
  • 4
5
votes
1 answer

Compiler discrepencies in deduction of template parameter of nested alias template

Consider the following code snippet where we we're trying to deduce the template parameter to the function foobar(): struct Bar { static constexpr auto size = 5; }; template > struct…
5
votes
0 answers

Template-template, variadic template, and deduction guide: compiler bug?

Consider the following heavily templated code: // Preamble #include #include #include #include #include // Rebind template template type traits template struct…
Vincent
  • 57,703
  • 61
  • 205
  • 388
5
votes
4 answers

Deduced conflicting types in template pack with reference

I'm working on a program with following structure: #include #include void fun(const std::string &text, int a, int b) { // (1) std::cout << text << a + b << std::endl; } template void…
5
votes
1 answer

Deducing a const l-value reference from a non-const l-value reference in C++ template

Suppose you have the following pair of functions: void f(const int&) { // Do something, making a copy of the argument. } void f(int&&) { // Do the same thing, but moving the argument. } They are fairly redundant—the only difference…
5
votes
2 answers

Template parameter can't be deduced on implicitly constructed argument

I would like to have the following code in c++17: #include #include #include #include class Foo; template class Bar { public: std::function m_fn; template
Javyre
  • 53
  • 1
  • 6