Search code examples
c++c++17typedeftype-alias

C++ type alias with using keyword


Is it possible to define such type alias with using keyword in c++? How would be the syntax? I have tried using const_type = typename const T::type it doesn’t work.

template <typename T>
    class DoubleBuffer {
    typedef T value_type;
    typedef T& reference;
    typedef T const & const_reference;
    typedef T* pointer;

    const_reference operator[](const size_t pos){
        ...
    }
};

Solution

  • template <typename T>
    class DoubleBuffer {
    public:
        using value_type = T;
        using reference = T&;
        using const_reference = T const &;
        using const_type = const typename T::type;
        using pointer = T*;
        //...
    };
    

    Live Demo

    Or:

    template <typename T>
    class DoubleBuffer {
    public:
        using value_type = T;
        using reference = T&;
        using const_reference = T const &;
        using const_type = const T;
        using pointer = T*;
        //...
    };
    

    Live Demo

    Depending on what T actually is - a struct/class with a nested type defined, or a standalone type.