Questions tagged [template-argument-deduction]

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

692 questions
6
votes
4 answers

disable template member function if return type is an array

https://www.godbolt.org/z/_4aqsF: template struct Container { template T find_if(TPred pred); // the culprit }; template Container MakeContainer(T const &) { return Container(); } int…
peterchen
  • 40,917
  • 20
  • 104
  • 186
6
votes
0 answers

Why must we specify template arguments for template base class when calling its constructor?

The following code snippet compiles only if I explicitly specify the template argument T for the Base struct in Derived ctor: template struct Base { Base(int) {} }; template struct Derived : Base { Derived(int i) :…
undermind
  • 1,779
  • 13
  • 33
6
votes
1 answer

Error when using operator << with implicitly converted non-fundamental data types

I have a struct that works as a wrapper for other types as follows: template struct A { A& operator=(const T& value){ m_value = value; return *this; } operator T() const { return m_value; } private: T…
dtell
  • 2,488
  • 1
  • 14
  • 29
6
votes
1 answer

User-defined vs automatic template deduction guides priorities

Let's say we have a class like this with a user-defined deduction guide: template struct Foo { Foo(Args&&...) { std::cout << "just Args: " << __PRETTY_FUNCTION__ << std::endl; } Foo(Args&&..., T&&) { std::cout…
0xd34df00d
  • 1,496
  • 1
  • 8
  • 17
6
votes
0 answers

Deducing class template parameters from variadic value-initialization (clang vs g++)

Consider the following variadic class template: template struct foo { template foo(Us...) { } }; If I try to instantiate foo in the following way, both g++(trunk) and clang++(trunk) are happy: auto o =…
6
votes
1 answer

For a type Class::Type, can I derive const Class::Type from a const Class?

I am implementing a container like: template class Container { public: using value_type = T; ... }; Is there a good way to derive a const value_type from const Container? Background: I have implemented iterator types via a…
jamesdlin
  • 81,374
  • 13
  • 159
  • 204
6
votes
2 answers

could not deduce template argument for T* from FieldType* (Visual C++ only)

This code compiles OK on g++ (Coliru), but not Visual C++ (rextester) - both online and my desktop. It is a simplified version of a much larger Visual Studio 2015 project. class AAA{ public: template static T* test(T* hqi){ …
javaLover
  • 6,347
  • 2
  • 22
  • 67
6
votes
2 answers

template deduction: const reference and const pointer

template void f(T t) {} int x = 1; const int & rx = x; const int * px = &x; f(rx); // t is int f(px); // t is const int *, instead of int *, WHY??? I'm confused now. According to Effective Modern c++, It’s important to recognize that…
Yves
  • 11,597
  • 17
  • 83
  • 180
6
votes
3 answers

Does C++11, 14 or 17 provide a way to get just the arguments out of a decltype()?

This question is very similar to: "Extract just the argument type list from decltype(someFunction)". I'm not sure the answers there work for what I'm intending though. I'd like to be able to create a template function that deduces the type of its…
Aaron Altman
  • 1,705
  • 1
  • 14
  • 22
6
votes
2 answers

C++ template parameter deduction for std::array with non size_t integer

I'm trying to adapt the solution presented in Avoiding struct in variadic template function to my need. However, I can't understand the the behavior of G++. Consider the following function: template int nextline(const…
hivert
  • 10,579
  • 3
  • 31
  • 56
6
votes
3 answers

c++ templated constructor error

More templated woes... I love C++, but sometimes I hate it. I cannot figure out why the compiler is complaining here, and what I can do about it. struct blah { template blah(void(*)(t), t){} }; void Func(int i) {} void Func2(int&…
5
votes
3 answers

Template argument deduction

I'm currently facing a problem I haven't been able to solve myself. Basically what I'm trying to do is implement some linq-like behaviour in C++. I'll start off with the code in my header: template class A, …
Tom Knapen
  • 2,277
  • 16
  • 31
5
votes
2 answers

Is it possible to deduce whether type is incomplete without compilation failure?

I want to achieve behavior like sizeof(complete_type) will return real sizeof, and sizeof(incomplete_type) - will be just 0 I need this to provide extended run time type information for IPC(inter-process) communication with the description structure…
dev_null
  • 1,907
  • 1
  • 16
  • 25
5
votes
1 answer

C++ Type vs. Non-Type Template Deduction Question

I have a question about C++ template deduction when there are two matches: one typed and the other non-typed. In the following situation: // First template void g(int a) { ... } // Second template void g(A a) { ... } int a =…
O.T.Vinta
  • 217
  • 1
  • 4
5
votes
0 answers

How to get the type of the first parameter of a dependent function pointer template parameter in C++11?

Current code: #include template std::pair f(T * const t) { // ... return std::make_pair(t, D); // need `T` and `D` here } void d(int *t); void g() { auto something = f
eepp
  • 7,255
  • 1
  • 38
  • 56