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
27
votes
2 answers

What is the point of `std::make_optional`

All the std::make_ are made redundant by C++17 with the introduction of Class template argument deduction (except make_unique and make_shared). So what is the point of std::make_optional? As far as I can tell it does the exact same thing as the…
bolov
  • 72,283
  • 15
  • 145
  • 224
25
votes
1 answer

Is Clang or GCC correct in rejecting/accepting this CTAD code?

Clang and GCC disagree on accepting this code. What is the standard required behavior? #include #include #include int main() { std::vector pairs = {std::pair{1,11},{2,22}, {3,33}}; for (const auto& p: pairs) { …
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
24
votes
1 answer

Why does std::vector CTAD accept both int and double?

#include #include #include int main() { std::vector v{2, 3.14}; std::cout << boost::typeindex::type_id_with_cvr().pretty_name() << '\n'; std::cout << "size: " << v.size() <<…
Tootsie
  • 655
  • 3
  • 11
18
votes
2 answers

Why does std::initializer_list in ctor not behave as expected?

#include int main() { auto v = std::vector{std::vector{}}; return v.front().empty(); // error } See online demo However, according to Scott Meyers' Effective Modern C++ (emphasis in original): If, however, one or more…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
10
votes
1 answer

CTAD and designated initializers in C++20

I have already stated confusion about CTAD with designated initializers in this question, but i have another confusion with a very similar code snippet template struct my_pair { int_t first; …
nnolte
  • 1,628
  • 11
  • 25
9
votes
1 answer

User defined-deduction guide for nested template-types

Q1 : Are user-defined deduction guides allowed at namespace scope ? In the example here, GCC and Clang does not produce the same behavior : https://godbolt.org/z/8W6hznEjo #include template struct some_type; template…
Guss
  • 762
  • 4
  • 20
9
votes
1 answer

Is std::make_move_iterator redundant since C++17's class template argument deduction?

Since C++11, to move append some vector y to another vector x, you can do: x.insert(x.end(), std::make_move_iterator(y.begin()), std::make_move_iterator(y.end())); With C++17 class template argument deduction it is possible to write this a bit more…
Ton van den Heuvel
  • 10,157
  • 6
  • 43
  • 82
7
votes
1 answer

C++ - Why does aggregate initialization not work with template struct

This code works, without having to specify a constructor: struct Foo { int a; int b; }; //... int a1, b1; Foo foo = {a1, b1}; If I make Foo a template, it doesn't work. template struct Foo { T1 a; …
Newline
  • 769
  • 3
  • 12
6
votes
0 answers

Should class template argument deduction (CTAD) work inside modules?

Given the following module // mod.cpp export module mod; export template struct something { constexpr something(T){} }; export template constexpr auto make_something(T t) { return something{t}; // uses…
Rumburak
  • 3,416
  • 16
  • 27
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

Why can't GCC compiler deduce one of the template parameter from std::array in alias template form

In C++20, alias templates can have implicit deduction guides if it is applied. Then, I have constructed a simple template alias which is ints: template using ints = std::array; but: ints{1, 2, 3, 4} doesn't work, and GCC…
Desmond Gold
  • 1,517
  • 1
  • 7
  • 19
5
votes
2 answers

Why can we not declare a deduction guide outside an inline namespace?

Example: namespace X{ inline namespace Y{ template struct A{ }; } } namespace X{ template A(std::vector) -> A; } This causes a compile error in Clang 11, which says "Deduction…
RedFog
  • 1,005
  • 4
  • 10
4
votes
1 answer

error: class template placeholder not permitted in this context

The following snippet compiles in gcc 12.1 but not in gcc 11.1. Unfortunately, I only have gcc 11 at hand as I'm crosscompiling for a microcontroller. Is there a way to make it work? Also what is this cryptic "use 'auto' for an abbreviated function…
glades
  • 3,778
  • 1
  • 12
  • 34
4
votes
1 answer

Template deduction for template aliasing with default value

According to P1814R0, the template deduction should work for alias with default value. With GCC 12.2(-std=c++20), the following code built successfully. However, in MSVC v19.33(/std:c++20) (which supports P1814R0), I got an error (10): error…
Yingnan Wu
  • 43
  • 4
4
votes
2 answers

Syntax options for accepting capturing lambdas as constructor args and storing them in class

I am trying to write a vector wrapper (indexed_vec) which stores objects of type ValueType but other datastructures (vectors of other types) refer to these by index (because iterators are clearly not stable). So when objects of ValueType are deleted…
Oliver Schönrock
  • 1,038
  • 6
  • 11
1
2 3 4 5