3

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. Sooner or later they will be tasked with implementing deduction guides. However, a note from cppreference makes me wonder whether it is at all possible. The note in question says:

These deduction guides are provided for std::basic_string to allow deduction from a std::basic_string_view. The size_type parameter type in (3) refers to the size_type member type of the type deduced by the deduction guide. These overloads participate in overload resolution only if Alloc satisfies Allocator.

Emphasis mine.

Can a user implement such a requirement? How can a programmer refer to aliases of the deduced type?

Fureeish
  • 12,533
  • 4
  • 32
  • 62
  • If you want to write a deduction guide for a class, it must be in the same namespace as that class is defined. If that namespace is `std`, then you will often run into standards-compliance issues, as most* of the time you are not allowed to add anything there. – AndyG Jan 09 '23 at 14:17
  • @AndyG I do not wish to extend anything `std::` related. I know that adding deduction guides to the `std` namespace is UB. I simply took inspiration from `std` type's deduction guide requirements and wondered whether it's possible to apply them to a user-defined type. – Fureeish Jan 09 '23 at 14:19

2 Answers2

2

The "type deduced by the deduction guide" is just the type to the right of the ->:

template< class CharT,
          class Traits,
          class Alloc = std::allocator<CharT>> >
basic_string( std::basic_string_view<CharT, Traits>, typename basic_string<CharT, Traits, Alloc>::size_type,
              typename basic_string<CharT, Traits, Alloc>::size_type, const Alloc& = Alloc() )
    -> basic_string<CharT, Traits, Alloc>;

Seems like it's just a shorthand for writing all the template arguments again, especially in places where the deduced type would be much longer, like unordered_set<typename std::iterator_traits<InputIt>::value_type, std::hash<typename std::iterator_traits<InputIt>::value_type>, std::equal_to<typename std::iterator_traits<InputIt>::value_type>, Alloc>.

Artyer
  • 31,034
  • 3
  • 47
  • 75
-1

First see documentation about User-defined deduction guides.

Then take a look on this example:

#include <iostream>
#include <string>
#include <vector>

template<typename T>
class Foo
{
public:
    template<typename C>
    Foo(C) {}

    template<typename C>
    Foo(C, typename C::size_type) {}
};

template<typename C>
Foo(C) -> Foo<typename C::value_type>;

template<typename C>
Foo(C, typename C::size_type) -> Foo<typename C::value_type>;

int main()
{
    std::string s;
    std::vector v{1, 3, 1};

    Foo foo{s}; // Here Foo<char> is used
    static_assert(std::is_same_v<Foo<char>, decltype(foo)>);

    Foo bar{v};
    Foo baz{v, 2};
    static_assert(std::is_same_v<Foo<int>, decltype(bar)>);
    static_assert(std::is_same_v<Foo<int>, decltype(baz)>);
}

https://godbolt.org/z/fsd6aMnY4

As you can see C type is used to guide to actually desired type. You can use something more complex then Foo<typename C::value_type>.

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • 2
    This misses the point. The desired deduction guide would look more like this: `Foo(C, Foo::some_type) -> Foo;` It has to use the deduced template as part of the parameter. – Nicol Bolas Jan 09 '23 at 15:19
  • @NicolBolas although you [can do that](https://godbolt.org/z/P8G94oTYr) – Caleth Jan 09 '23 at 15:50
  • @MarekR: Not `C::value_type`; `Foo::value_type` (I said that wrong). The point is that the class being deduced (`Foo`) is also part of the name of a type's parameter. – Nicol Bolas Jan 09 '23 at 16:05
  • @NicolBolas problem is that question is not very well defined. Now I regret deleting my comment under question posted before answer requesting a code he wishes to add this deduction guide. I do not see a problem adding deduction guide in my form or in your form. This is question what is the final goal. – Marek R Jan 09 '23 at 16:14
  • @MarekR: I think you're over-thinking the question. The problem is that the answer to the literal text is so obvious that you're thinking that there must be more to it than that. But there isn't. The OP simply has not tried the obvious solution. – Nicol Bolas Jan 09 '23 at 16:26