Questions tagged [deduction-guide]

30 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
12
votes
1 answer

How to write deduction guidelines for aliases of aggregate templates?

With C++20, it is possible to have deduction guidelines generated for an alias template (See section "Deduction for alias templates" at https://en.cppreference.com/w/cpp/language/class_template_argument_deduction). Yet, I could not make them work…
8
votes
1 answer

The deduction guide for std::array

In the C++ 17 and C++ 20 Working Drafts of the C++ Standard the deduction guide for the class template std::array is defined the following way template array(T, U...) -> array; As a result for example this…
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
7
votes
1 answer

Do deduction guides require noexcept specifiers?

For some reasons, I've always thought that deduction guides must have the same noexcept-ness of the constructor to which they refer. For example: template struct clazz { clazz(const T &) noexcept {} }; clazz(const char &) noexcept…
skypjack
  • 49,335
  • 19
  • 95
  • 187
6
votes
2 answers

Multi-dimension array template with dimension deduction

I want to make an NDArray template which has a fixed dimension, but can be resized across each dimension. My question is how to make it be able to deduce the dimensions in the constructor according to how many pair of {} is used? The elements in the…
sz ppeter
  • 1,698
  • 1
  • 9
  • 21
6
votes
1 answer

Can I add a deduction guide to `std` namespace?

Suppose I want to make a new deduction guide making the following possible ? std::string str; std::basic_string_view sv = str; Would that be an Ok customization ?
5
votes
0 answers

C++17 Deduction Guide defined inside a class is not effective inside this class, but useful outside the class

#include struct A { }; struct B { }; struct Test { template struct overloaded : Ts { }; // 1st Deduction Guide template overloaded(Ts)->overloaded; // 2nd Deduction Guide for class…
4
votes
1 answer

Abbreviated function template syntax for user-defined class template argument deduction guide

I'm writing a deduction guide in the style of an abbreviated function template, but I'm not sure if it's allowed. It compiles on gcc and clang, but not msvc. The error is: error C3539: a template-argument cannot be a type that contains…
Petwoip
  • 1,365
  • 2
  • 17
  • 25
3
votes
2 answers

Can a user refer to a deduced type of a deduction guide?

std::basic_string's deduction guides allow the user to use the std::basic_string name without specifying its template parameters. Users are also allowed to create their own deduction guides. Assume that the user wants to recreate std::basic_string.…
Fureeish
  • 12,533
  • 4
  • 32
  • 62
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

User-defined deduction guides for non-type template parameters

I have the beginnings of a matrix class. Here's the code- template class mat { public: mat() : values(h, std::vector(w)) { if (w == h) { int x = 0; for (int y = 0; y < h; y++) { …
Jcsq6
  • 474
  • 1
  • 4
  • 15
3
votes
0 answers

any alternative deduction to class template argument deduction?

I have a class template: template class iterator { explicit iterator(T*); }; I tried using the c++17 feature Class template argument deduction (CTAD) to deduce T, but I can't do so, since there is no way to deduce N…
user1095108
  • 14,119
  • 9
  • 58
  • 116
3
votes
1 answer

How can I create deduction guides for template aliases in C++20?

Suppose I have a class/struct template together with an explicit deduction guide for its constructor. Let this class have two template parameters of which one can get deduced by the deduction guide, for the other it can not. template
Jodocus
  • 7,493
  • 1
  • 29
  • 45
3
votes
1 answer

What are my options for adding deduction guides for stl types

This post already explains how adding deduction guides in the std namespace is undefined. Now, what I would really like to do is this: namespace std { // undefined behavior template array(char const*, U...) -> array
Andreas DM
  • 10,685
  • 6
  • 35
  • 62
1
2