0

In the following code:

template<typename T, int N>
struct Buffer {
    using value_type = T;
    constexpr int size() { return N; }
    T[N];
};

can you explain to me what this line does and what happens if we remove it?

using value_type = T;
Jarod42
  • 203,559
  • 14
  • 181
  • 302
Fractale
  • 1,503
  • 3
  • 19
  • 34
  • Are your more familiar with (old) `typedef`? – Jarod42 Apr 06 '21 at 13:31
  • 1
    TL;DR of the dupe: `using value_type = T;` creates an alias for `T` called `value_type` – NathanOliver Apr 06 '21 at 13:32
  • https://en.cppreference.com/w/cpp/language/type_alias – Jarod42 Apr 06 '21 at 13:32
  • 1
    It will basically make `Buffer::value_type` equivalent to `Buffer::T` – Cory Kramer Apr 06 '21 at 13:33
  • 1
    @CoryKramer: there is no `Buffer::T`, precisely... – Jarod42 Apr 06 '21 at 13:34
  • 1
    If you remove it, it will be difficult to get to the Buffer template's T parameter. By providing the `using`, it makes generic programming and template meta programming much easier. – Eljay Apr 06 '21 at 13:36
  • And if your next question is why have a `value_type`, see: https://stackoverflow.com/questions/44571362/what-is-the-use-of-value-type-in-stl-containers/44571482#44571482 – NathanOliver Apr 06 '21 at 13:40
  • @NathanOliver If I have T why would I want to rename it value_type? is value_type[N]; work here? – Fractale Apr 06 '21 at 13:49
  • 2
    @Fractale You have `T` inside the class, but people outside the class don't. They can't do `Buffer::T` to get the template type. – NathanOliver Apr 06 '21 at 13:51
  • @NathanOliver Each vector with a different T will make a different value_type right? Is value_type a field? Is it useful only for template that uses Buffer and wants to know T? Can value_type be used somewhere else than another template? – Fractale Apr 06 '21 at 14:14
  • 2
    `value_type` is a type alias. It doesn't take up any space. For each `Buffer`, you will have a `value_type` for each different `T` you use. `value_type` can be used outside template code, but template code is the most common use as that is where it's needed the most. When you aren't in a template, you should know what type of `Buffer` you have, so `value_type` isn't so useful. In template code, you might not even know what type of container you have, but if all containers have a `value_type` member, then you at least have a way to get the type of the elements in the container. – NathanOliver Apr 06 '21 at 14:18
  • You might have a situation like `std::string`, which is an alias for `std::basic_string, std::allocator>`. You could find `std::string::value_type` in non-template code that uses strings – Caleth Apr 06 '21 at 14:22
  • @NathanOliver in a template you will use O::value_type as a type to declare a variable because you don't have any information on O. if this function took a Buffer as a parameter you can just use T as normal. So value_type is useful when you want to generalize more and leave the Buffer type outside the template definition. Is it correct? – Fractale Apr 06 '21 at 14:41
  • 1
    @Fractale Correct. Imagine you have `template something sum(const Container& cont)`. Here you only know you have a container, so `something` needs to be replaced with `typename Container::value_type` and inside the function, the variable you do your accumulation needs to be declared like `typename Continer::value_type accumulator{};` – NathanOliver Apr 06 '21 at 14:43
  • @NathanOliver concerning the non-template code, How do I access the value? And How to use it? Is it useful for something? Is it something similar to the following code? Buffer b; if(b::value_type == int) {cout<<"I have a vector of int!"} – Fractale Apr 06 '21 at 15:03
  • 1
    @Fractale Like I said, there is no real point to it outside of template code. If you have a `Buffer`, then you already know everything you need to know about it. – NathanOliver Apr 06 '21 at 15:05
  • @NathanOliver Thank you for your really clear and eye-opening explanations. – Fractale Apr 06 '21 at 15:06

0 Answers0