Class Template Argument Deduction, introduced in C++17, allows deducing missing template arguments for class template instantiation based on the initializer.
Questions tagged [ctad]
66 questions
2
votes
1 answer
CTAD fails for templated base class
Consider the following simple construct. I derive a class type EntityView from Entity which allows me to specify an allocator if I want to, and if I don't it should fall back to the defaulted template parameter. Yet the compiler complains that it…

glades
- 3,778
- 1
- 12
- 34
2
votes
1 answer
why does CTAD result in a vector with iterators, not integers?
#include
#include
#include
int main()
{
std::vector initv { 42, 31, 7 };
std::vector v1{initv.begin(), initv.end()}; // CTAD
std::vector v2{initv.begin(), initv.end()};
…

Tootsie
- 655
- 3
- 11
2
votes
2 answers
Can a class template have more than one user defined deduction guide for the same constructor?
Is it valid to have more than one user defined deduction guide for the same constructor in a class template?
For example:
template
class A {
T t;
public:
A(T t): t(std::move(t)) { /* ... */ }
};
template
requires (…

Amir Kirsh
- 12,564
- 41
- 74
2
votes
2 answers
Inherit CTAD constructor
I have derived from std::tuple and but was unable construct the derived class from an initializer list due to issues with class template argument deduction. Is there a better way to construct such a class beyond just giving it an already constructed…

Tom Huntington
- 2,260
- 10
- 20
2
votes
0 answers
Deduce template parameter of class member from constructor of class
I have a class A which contains a templated member B whose exact type should be deduced from A's constructor. The way this is supposed to work is that, as shown in the below example, B can be instantiated with either 1 or 2 parameters to its…

glades
- 3,778
- 1
- 12
- 34
2
votes
0 answers
Is it possible to make CTAD accept template type alias?
C++ introduced class template argument deduction so instead of
std::array arr{1,2,3,...};
std::vector vec{0.0, 0.1, 0.01, ...};
std::pair pair {1, 1.1};
we can write cleaner code
std::array arr{1,2,3,...};
std::vector…

Sugar
- 490
- 9
- 22
2
votes
2 answers
SFINAE to detect the explicitness of a CTAD deduction guide
Little-known feature of CTAD (class template argument deduction) in C++17: you can mark user-defined deduction guides as explicit.
(Godbolt.)
template struct A { A(int); }; A(int) -> A;
template struct B { B(int); };…

Quuxplusone
- 23,928
- 8
- 94
- 159
2
votes
1 answer
How to write a deduction guide for union type
Let's suppose we have this template for a union of a struct and an array of bytes of the same size
template
union point {
struct { T x, y; } coord;
static constexpr size_t buffer_size = sizeof(coord);
unsigned char…

Vladimir
- 96
- 6
2
votes
1 answer
CTAD fail to deduce template argument with SFINAE in partial specialization
I tried using CTAD with SFINAE in partial specializations, but it doesn't compile unless I add a seemingly useless deduction guide. What's the reason/limitation behind?
template
struct A;
template

sz ppeter
- 1,698
- 1
- 9
- 21
2
votes
2 answers
Array size template deduction
I have a class that stores an std::array.
The size of the array is evaluated in compile time, this is because the application runs on an embedded device so no dynamic allocations :(.
The code looks something like this:
template
class…

Eyal Kamitchi
- 159
- 9
1
vote
1 answer
Passing designated initializers for a template class to a template function
My goal is to create an API interface that looks like that this:
struct myAB{ int a,b; };
void function(myAB ab) {}
...
function({.a = 1, .b = 3});
The above works just fine. But if I want struct AB to have a templated type, CTAD fails.
template…

dada_dave
- 493
- 4
- 13
1
vote
0 answers
Can user-defined CTAD parameters be declared with auto&&?
LiveDemo: https://godbolt.org/z/daP7c8av6
template
struct A {
A(T);
};
// OK
template
A(T&&) -> A;
template
struct B {
B(T);
};
// GCC&Clang: OK
// MSVC: error C3539: a template-argument cannot be a…

VainMan
- 2,025
- 8
- 23
1
vote
1 answer
Constrain an input type in deduction guide
I want my class object's ctor to accept a functor that is able to be converted to a std::packaged_task whereas R is auto deduced from the functor's return value. Here's what I've got so far:
Demo
#include
#include
#include…

glades
- 3,778
- 1
- 12
- 34
1
vote
1 answer
Deduction guide based on number of parameters passed to constructor
Here's something I'm trying that doesn't seem to work: I want to toggle a compile time switch based on how a class object is instantiated. If there's just one constructor argument, the LengthOpt should equal to false, otherwise to true (my…

glades
- 3,778
- 1
- 12
- 34
1
vote
3 answers
partial class template argument deduction in C++
I have a templated class but only part of the template arguments can be deduced from the constructor.
Is there a way to provide the rest of the template arguments inside angle brackets when calling the constructor?
Assume we're using…

Zamfir Yonchev
- 151
- 1
- 10