Questions tagged [ctad]

Class Template Argument Deduction, introduced in C++17, allows deducing missing template arguments for class template instantiation based on the initializer.

66 questions
4
votes
1 answer

Redundant parentheses in the declarator of a variable with deduced class template specialization type

The following program template struct S {}; S (s); is compiled by GCC with only a warning about the redundant parentheses around the declarator. However, Clang gives a hard error for the declaration error: cannot use parentheses…
cigien
  • 57,834
  • 11
  • 73
  • 112
4
votes
1 answer

User-defined deduction guide for std types

For some reason there is still lack of expected CTAD for std::initializer_list in clang: std::initializer_list l{1,2,3}; // error in clang Adding a user-defined guide like the following can fix the issue: namespace std { template
αλεχολυτ
  • 4,792
  • 1
  • 35
  • 71
4
votes
1 answer

Class Template Argument Deduction - clang and gcc differ

The following code compiles with gcc but not with clang: template class number { T num; public: number(T num = 0): num(num) {} template friend auto add(T1 a, T2 b); }; template
Amir Kirsh
  • 12,564
  • 41
  • 74
3
votes
2 answers

Can CTAD be used inside a member of the template class?

C++ has this helpful feature that says that the template parameter is implicit in the code inside a template class A. However, for construction, this seems to clash with CTAD. How can I make CTAD take precedence? For example, here, there is an error…
alfC
  • 14,261
  • 4
  • 67
  • 118
3
votes
1 answer

Why does C++ span's C style array constructor need type_identity_t?

The C style array constructor for span is specified as follows template constexpr span( type_identity_t (&arr)[N]) noexcept; Why is type_identity_t necessary? instead of just: template constexpr span( …
cbhattac
  • 121
  • 4
3
votes
1 answer

Why do you no longer need to use a deduction guide to create a lambda overload set in C++20?

In Timur Doumler's talk at CppCon 2022, "C++ Lambda Idioms", at around 45:19 he is discussing the lambda overload trick and says something like In C++17, we had to write a deduction guide to make this work -- which is like two more lines of code --…
jwezorek
  • 8,592
  • 1
  • 29
  • 46
3
votes
0 answers

Deduction guide needed if outer class is a template?

The following code struct some{}; some thingy; #ifdef OH template #endif struct Wrapper { template struct Checker { Checker(S &) {} }; template friend void check(S& strm,…
Bulletmagnet
  • 5,665
  • 2
  • 26
  • 56
3
votes
1 answer

Does providing an explicit deduction guide disable the generation/formation of implicit deduction guides

I am reading about deduction guides in C++17. So say we have the following example: template struct Custom { }; template struct Person { Person(Custom const&); Person(Custom&&); }; template
Jason
  • 36,170
  • 5
  • 26
  • 60
3
votes
1 answer

clang vs gcc - CTAD of struct deriving from template parameter

Consider the following code: template struct D : B { }; D d{[]{ }}; gcc 12.x accepts it and deduces d to be D as expected. clang 14.x rejects it with the following error: :4:3: error: no viable…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
3
votes
2 answers

How can I give two compatible names to a C++ class template with deduction guides?

If I have a widely-used class template called Foo that I want to rename to Bar without having to update all of its users atomically, then up until C++17 I could simply use a type alias: template class Bar { public: // Create a Bar…
jacobsa
  • 5,719
  • 1
  • 28
  • 60
3
votes
1 answer

c++ deduce bool class template argument by constructor choice

I am trying to deduce a bool template argument by the choice of the class constructor. A simple example: template class Subrange { public: Subrange(A a) requires (not Condition); /* create Subrange */ …
Lessi
  • 165
  • 7
3
votes
2 answers

Type deduction for variadic templates

Problem: I want to have a deduction guide for a class that takes a variable number of objects that are constructed by a variadic template. E.g. template struct y { using values_t = std::tuple; values_t values; …
Arwed Mett
  • 2,614
  • 1
  • 22
  • 35
3
votes
2 answers

Class Template Argument Deduction for template dependent parameter

Let's start with a simple add method for class number: class number { int num; public: number(int num = 0): num(num) {} operator int() const { return num; } }; number add(number t1, number t2) { return t1 + t2; } int main() { …
Amir Kirsh
  • 12,564
  • 41
  • 74
3
votes
1 answer

Is there a way to have an alias of a template and preserve Class Template Argument Deduction?

#include template using vec = std::vector; int main() { std::vector a{2,3}; // vec b{2,3}; // not going to work } Are we still forced to use macros? There are so many disadvantages in using them...
Hrisip
  • 900
  • 4
  • 13
3
votes
3 answers

Template argument deduction fails when using braced initializer list

I'm trying to use template argument deduction in the 'perpendicular()' function: #include template struct offset { component x; component y; }; template offset(component x,…