Questions tagged [template-argument-deduction]

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

692 questions
0
votes
2 answers

How to keep signature of a callable argument visible in a C++ template?

The function f() below takes the callable argument fc whose signature is nicely visible in the function definition. // scaffolding for function definition template struct Identity { using type = T; }; // still scaffolding for…
Kemal
  • 849
  • 5
  • 21
0
votes
1 answer

Why doesn't C++17 template class deduction work when I supply some of the template parameters?

C++17 has introduced class template argument deduction. While most of the time it's nothing more than a syntactic sugar, there are cases when it really comes to the rescue, especially in the generic code, like in this case. Another nice application…
Vasiliy Galkin
  • 1,894
  • 1
  • 14
  • 25
0
votes
0 answers

Template argument deduction fails when using a braced init expression

template void printPair(const std::pair &p) { std::cout << p.first << ", " << p.second << std::endl; } int main() { printPair({1, 1}); return 0; } When I try to compile that code, the compiler is…
user1000039
  • 785
  • 1
  • 7
  • 19
0
votes
1 answer

Passing std algorithm. Template deducing

I was trying to make this small example work #include #include #include int main() { std::vector v{'a', 'b', 'c', 'd'}; std::transform(v.begin(), v.end(), v.begin(), std::toupper); …
0
votes
1 answer

Cannot deduce template parameter 'N'

I tried to reduce this to its minimum: #include template void f(int, std::array const & = std::array()) { } int main() { f(10); } array_test.cpp:4:6: note: template argument…
Germán Diago
  • 7,473
  • 1
  • 36
  • 59
0
votes
1 answer

C++: how can I overload a function so that it can take any functor, including pointer to member functions?

In order to broaden my understanding of C++11, I'm experimenting with writing functional helpers and seeing if I can make calling them less verbose. Right now I'm trying to write a some function that returns true if any item in a collection passes a…
Bri Bri
  • 2,169
  • 3
  • 19
  • 44
0
votes
2 answers

How to 'help' the compiler to deduce function template return type from a template parameter which is a function?

To de-clutter code from strtoxx calls, but still have them inlined, I would like to have a function template like: template static auto StrToNum( const string& s ) { char* pEnd; return STR_TO_NUM( s.c_str(), &pEnd, 10…
Aelian
  • 665
  • 1
  • 8
  • 13
0
votes
1 answer

Why can't (or doesn't) the compiler deduce the type parameter of static_cast?

I have some (legacy) code that looks like: void castFoo(string type, void* foo) { FooA* foo_a = NULL; FooB* foo_b = NULL; if (type == "A") { foo_a = static_cast(foo); } else { foo_b = static_cast(foo); } // now do…
Patrick Collins
  • 10,306
  • 5
  • 30
  • 69
0
votes
2 answers

How does the compiler deduce array size when defined as a template parameter?

I am wondering how, in the following piece of code, the compiler deduces the arrsize template argument from the T (&arr)[arrsize] function argument. For example, when I pass a 4-element array to it, without mentioning the number 4 in my call to the…
0
votes
1 answer

Variadic template deduction/matching for a closure class

I'm trying to create sort of a closure callback class and run into some problems with template parameter deduction and matching (The class works as follows: Upon construction, it will take a member or non-member function as well as a variable…
0
votes
1 answer

Implicit conversion may happen during template substitution?

I always thought that with templated functions, no implicit conversion may happen and the types of the arguments must exactly match the templated types of the parameters, or else the template argument deduction will fail. Well, it seems I was…
user4385532
0
votes
1 answer

C++, match custom placeholders with function arguments

I am trying to write the piece of code which will do the following: let's assume we have a call of custom bind function auto bind_obj = bind(some_func, _1, "test") and after we have auto res = bind_obj(42) where the function some_func: int…
0
votes
2 answers

'template argument deduction/substitution failed' after addition of an argument

I need template function which will call other function with arguments taken from std::tuple. I had written some code, which compiles properly: #include template void foo(const std::tuple &,…
Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65
0
votes
1 answer

Deduction fails in VisualStudio 2013 with first template argument being a specific type (which in turn is arbitrarily templated)

I want to have a certain function that does a computation if the Argument is a certain template with a specific first template parameter (arbitrarily templated). Consider these classes template struct A { }; template struct B {…
Pixelchemist
  • 24,090
  • 7
  • 47
  • 71
0
votes
3 answers

Could not deduce template type for variadic template

I have created simple callback based event manager and it works, but I have some errors with zero template arguments. class event_manager { public: template static void register_event(const unsigned long e,…